Home > database >  Mysql data access to the same category 5, lack of access to other categories make up five data
Mysql data access to the same category 5, lack of access to other categories make up five data

Time:10-15

I recently met a problem, to a certain category of article 5 of the data, if the classification data less than five will take the other classification data, make up 5, the SQL should be how to write?

CodePudding user response:

Thinking:

Select * from
(select *, 0 as g from table
Where classification=XXX
Limit of 5
Union all
Select *, 1 as g from table
Where classification=yyy
Limit 5) a
The order by g
Limit of 5
  • Related