开发者

How can I ORDER BY according to some boolean logic?

开发者 https://www.devze.com 2023-03-10 20:02 出处:网络
Say I have columns \'a\' and \'b\'.A record is deemed \'ready\' if !a || b.Ho开发者_JS百科w can I sort by this ready condition?I\'m really rusty with SQL and I can\'t recall what would have been the b

Say I have columns 'a' and 'b'. A record is deemed 'ready' if !a || b. Ho开发者_JS百科w can I sort by this ready condition? I'm really rusty with SQL and I can't recall what would have been the best way to do this. My guess is that I would be able to add a column with the boolean result and then sort by that column, but I've tried searching and can't seem to find what I'm looking for.


order by case when !a || b then 0 else 1 end


You can put expressions in the ORDER BY clause.

ORDER BY (!a || b) ASC


So you just want all rows where !a or b is true first? If so order by !a and then by b.

0

精彩评论

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