开发者

MySQL Query order by if null

开发者 https://www.devze.com 2023-03-19 10:35 出处:网络
I am trying to build a MySQL query that has an order by statement. This is what I am trying to do: SELECT *

I am trying to build a MySQL query that has an order by statement. This is what I am trying to do:

SELECT * 
FROM tbl_product 
ORDER BY retail_price ONLY IF wholesale_price IS NULL O开发者_运维知识库THERWISE 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.

Any advice is appreciated.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消