简体   繁体   English

如何使用空列值对mysql表排序

[英]How to sort a mysql table with null column values

I have a table with following schema. 我有一个具有以下架构的表。 I need to sort table by points , and if rows wiith img_link present (not null ) come first also. 我需要按对表进行排序,如果存在img_link (不为null )的行也排在第一位。 Simply need to do is - sort by int column , then by varchar column . 只需要做的就是按int列排序,然后按varchar列排序。

+-----+--------+-----------+-----------+
| id  | name   | img_link  |  points   |
+-----+--------+-----------+-----------+
| 11  | smpl   | path.jpg  |  10       |
+-----+--------+-----------+-----------+
| 12  | main   |  null     |  20       |
+-----+--------+-----------+-----------+
| 13  | abcd   |  null     |  10       |
+-----+--------+-----------+-----------+
| 14  | xyls   | img_.png  |  10       |
+-----+--------+-----------+-----------+

need a result like 需要一个类似的结果

+-----+
| id  |
+-----+
| 12  |
+-----+
| 11  |
+-----+
| 14  |
+-----+
| 13  |
+-----+

Thanks in advance.. 提前致谢..

尝试这个

SELECT * FROM table_name ORDER BY points DESC ,ISNULL(img_link), img_link 

You basically wrote out in words exactly what you need to do. 基本上,您用言语准确地写下了您需要做的事情。

SELECT id FROM someTable ORDER BY points DESC, img_link DESC;

DEMO DEMO

Another way is 另一种方法是

select *
from table
order by `points` desc,
if(img_link = '' or img_link is null,1,0)

DEMO DEMO

尝试这个:

select * from tabalename where img_link is not null order by point desc union select * from tabalename where img_link is null order by point desc
SELECT * FROM my_table ORDER BY points DESC, img_link IS NULL, img_link DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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