简体   繁体   中英

MySQL SELECT and Subset condition

So there are one major WHERE clause condition that I want my result from , but I have another condition such that I would like result that satisfy both conditions to appear first in the result array, follow by result that only satisfy the major condition. One possible way may be to do the selection based on the major result first and then manipulate using the second condition. But I would like to see if there is a way to do it by mysql select statement itself.

After re-reading your question, I understand that you don't want to filter results by second criteria, but to simply re-order them. In that case, use custom ORDER BY , something like:

SELECT *
FROM mytable
WHERE x = 'major'
ORDER BY (y = 'minor') DESC

use with block to multipal where close

Blockquote

with t as {

select * from table a where a.columnname = 1

}, p as {

select * from table b where b.columnname = 2

}

You can order by your conditions.

select some_columns 
  from a_table 
 where major_condition
 order by if(minor_condition, 1, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM