I am trying to build a MySQL query that has an order by statement. This is what I am trying to do:
我正在尝试构建一个具有order by语句的MySQL查询。这就是我想要做的:
SELECT *
FROM tbl_product
ORDER BY retail_price ONLY IF wholesale_price IS NULL OTHERWISE ORDER BY wholesale_price.
I have no idea where to start. I found an article that uses ORDER BY COALESCE but I also found that this could have performance issues.
我不知道从哪里开始。我找到了一篇使用ORDER BY COALESCE的文章,但我也发现这可能存在性能问题。
Any advice is appreciated.
任何建议表示赞赏。
1 个解决方案
#1
24
SELECT *
FROM tbl_product
ORDER BY ifnull(wholesale_price, retail_price);
Note that you don't need to select the value you're ordering by - it can be any expression
请注意,您无需选择要排序的值 - 它可以是任何表达式
#1
24
SELECT *
FROM tbl_product
ORDER BY ifnull(wholesale_price, retail_price);
Note that you don't need to select the value you're ordering by - it can be any expression
请注意,您无需选择要排序的值 - 它可以是任何表达式