开发者

MySQL IFNULL on 4 or 5 columns

开发者 https://www.devze.com 2023-03-19 18:45 出处:网络
I have a statement that is: ORDER BY IFNULL(rrp, wholesale) ASC ...but I wanted to know if there开发者_如何学Python is a way to have an IFNULL statement that checks 3 or 4 or 5 columns? All from th

I have a statement that is:

ORDER BY IFNULL(rrp, wholesale) ASC

...but I wanted to know if there开发者_如何学Python is a way to have an IFNULL statement that checks 3 or 4 or 5 columns? All from the same table.


Use COALESCE

e.g:

SELECT ....
  FROM ....
ORDER BY COALESCE(rrp, wholesale, column3, column4)


You could chain them up, so the expression picks the first not-null value from a list of columns:

ORDER BY IFNULL(IFNULL(IFNULL(rrp, wholesale), some_column), some_other_column) ASC


Try using COALESCE; it may give you what you need.

0

精彩评论

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