How to sort SQL results if one column is empty?
P粉969666670
P粉969666670 2024-04-05 00:39:32
0
1
317

I have questions about the sorting of query results. I have 3 columns that need to be sorted:

ORDER BY level DESC, logo DESC, bName ASC

I wanted to order the highest "level" via DESC first and this worked.

After that I want to order if "logo" is empty but not the logo content which my query executes.

Finally I want to order by bName ASC

So the output I want is this:

1. Level-3 has logo bName a-z
2. Level-3 has no Logo bName a-z
3. Level-2 has logo bName a-z
4. Level-2 has no Logo bName a-z
5. Level-1 has logo bName a-z
6. Level-1 has no Logo bName a-z

Is it possible to accomplish this with just one clean query?

Corresponding example input:

level LOGO bName
3 Test.jpg Test name
2 Test name
1 12test.jpg Another test name
1 3test.jpg anonymous
2 John Doe
2 Dodo
3 5test.jpg Test name
1 sdsd-test.jpg Test name

In some cases the column logo is empty (e.g. = '' ), but not NULL

P粉969666670
P粉969666670

reply all(1)
P粉745412116

You haven't provided information about your data and what "logo is empty" actually means, but the principle is:

ORDER BY 
  level DESC, 
  case when logo  '' then 1 else 2 end,
  bName ASC;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!