MySQL ORDER BY x-y/x ASC

半腔热情 提交于 2019-12-13 18:40:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!