问题
I want to order ascending a MySQL query by this rule: x-y/x
x = price
y = price2
price and price2 are the columns from the MySQL table.
I have this query but unfortunately doesn`t work.
SELECT *
FROM albums
WHERE price2 > 1
ORDER BY price - price2 / price ASC
Thanks for attention.
回答1:
try this out:
$SQLquery = "SELECT * FROM albums WHERE price2 > 1 ORDER BY (price-price2)/price ASC";
or
$SQLquery = "SELECT * FROM albums WHERE price2 > 1 ORDER BY ((price-price2)/price) ASC";
回答2:
You should try:
SELECT * FROM albums
WHERE price2 > 1
ORDER BY ((price - price2) / price) ASC
or
SELECT albums.*, ((price - price2) / price) myvar FROM albums
WHERE price2 > 1
ORDER BY myvar ASC
回答3:
Order By works based on specified column. You can create a view or temporary table from your base table containing a new column with the value based on that formula and then sort that column.
来源:https://stackoverflow.com/questions/10283204/mysql-order-by-x-y-x-asc