user1906418
user1906418

Reputation: 139

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.

Upvotes: 1

Views: 908

Answers (3)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39951

You can order by your conditions.

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

Upvotes: 0

Jignesh.Raj
Jignesh.Raj

Reputation: 5987

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

}

Upvotes: 0

mvp
mvp

Reputation: 116098

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

Upvotes: 1

Related Questions