查询排序结果

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查询排序结果相关的知识,希望对你有一定的参考价值。

查询排序结果

  1. SELECT * FROM driver_log ORDER BY name;
  2. SELECT * FROM driver_log ORDER BY name DESC;
  3. SELECT * FROM driver_log ORDER BY name,trav_date;
  4. SELECT * FROM driver_log ORDER BY name DESC,trav_date;

表达式进行排序

  1. SELECT t,srcuser,FLOOR((size+1023)/1024) FROM mail WHERE size > 50000 ORDER BY FLOOR((size+1023)/1024);
  2. SELECT t,srcuser,FLOOR((size+1023)/1024) AS kilobytes FROM mail WHERE size > 50000 ORDER BY kilobytes;

查询结果中不包含排序列

  1. SELECT t,srcuser,CONCAT(FLOOR((size+1023)/1024),‘K‘) AS size_in_K FROM mail WHERE size > 50000 ORDER BY size_in_K;
  2. SELECT t,srcuser,CONCAT(FLOOR((size+1023)/1024),‘K‘)AS size_in_K FROM mail WHERE size > 50000 ORDER BY size;
  3. SELECT name,jersey_num FROM roster;
  4. SELECT name,jersey_num FROM roster ORDER BY jersey_num;
  5. SELECT name,jersey_num FROM roster ORDER BY jersey_num+0;
  6. SELECT t,CONCAT(srcuser,‘@‘,srchost) AS sender,size FROM mail WHERE size > 50000 ORDER BY srchost,srcuser;
  7. SELECT last_name,first_name FROM name ORDER BY last_name,first_name;

控制字符串排序大小写区分

  1. SELECT ci_str FROM str_val ORDER BY ci_str;
  2. SELECT cs_str FROM str_val ORDER BY cs_str;
  3. SELECT bin_str FROM str_val ORDER BY bin_str;
  4. SELECT ci_str FROM str_val ORDER BY ci_str COLLATE latin1_general_cs;
  5. SELECT cs_str FROM str_val ORDER BY cs_str COLLATE latin1_swedish_ci;
  6. SELECT bin_str FROM str_val ORDER BY CONVERT(bin_str USING latin1) COLLATE latin1_swedish_ci;

日期排序

  1. SELECT * FROM mail WHERE srcuser = ‘phil‘;
  2. SELECT * FROM mail WHERE srcuser = ‘phil‘ ORDER BY t DESC;
  3. SELECT * FROM mail ORDER BY TIME(t);
  4. SELECT date,description FROM occasion ORDER BY date;
  5. SELECT * FROM mail WHERE srcuser = ‘phil‘;
  6. SELECT * FROM mail WHERE srcuser = ‘phil‘ ORDER BY t DESC;
  7. SELECT DAYNAME(date) AS day,date,description FROM occasion ORDER BY DAYOFWEEK(date);
  8. SELECT DAYNAME(date),date,description FROM occasion ORDER BY MOD(DAYOFWEEK(date)+5,7);

利用列值子字符串排序

  1. SELECT * FROM housewares;
  2. SELECT id,LEFT(id,3) AS category,MID(id,4,5) AS serial,RIGHT(id,2) AS country FROM housewares;
  3. SELECT * FROM housewares ORDER BY LEFT(id,3);
  4. SELECT * FROM housewares ORDER BY MID(id,4,5);
  5. SELECT * FROM housewares ORDER BY RIGHT(id,2),MID(id,4,5);
  6. SELECT category,serial,country,CONCAT(category,serial,country) AS id FROM housewares2 ORDER BY category,country,serial;
  7. SELECT id,LEFT(SUBSTRING(id,4),CHAR_LENGTH(SUBSTRING(id,4)-2)) FROM housewares3;
  8. SELECT id,SUBSTRING(id,4),SUBSTRING(id,4,CHAR_LENGTH(id)-5) FROM housewares3;
  9. SELECT * FROM housewares3 ORDER BY SUBSTRING(id,4,CHAR_LENGTH(id)-5);
  10. SELECT * FROM housewares3 ORDER BY SUBSTRING(id,4)+0;
  11. SELECT *FROM housewares4;
  12. SELECT id,SUBSTRING_INDEX(SUBSTRING_INDEX(id,‘-‘,2),‘-‘,-1) AS segment2,SUBSTRING_INDEX(SUBSTRING_INDEX(id,‘-‘,4),‘-‘,-1) AS segment4 FROM housewares4;
  13. SELECT * FROM housewares4 ORDER BY SUBSTRING_INDEX(SUBSTRING_INDEX(id,‘-‘,2),‘-‘,-1);
  14. SELECT name FROM hostname ORDER BY name;
  15. SELECT name,SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(‘..‘,name),‘.‘,-3),‘.‘,1) AS leftmost,SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(‘.‘,name),‘.‘,-2),‘.‘,1) AS middle,SUBSTRING_INDEX(name,‘.‘,-1) AS rightmost FROM hostname;
  16. SELECT name FROM hostname ORDER BY SUBSTRING_INDEX(name,‘.‘,-1),SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(‘.‘,name),‘.‘,-2),‘.‘,1),SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(‘..‘,name),‘.‘,-3),‘.‘,1);

按数字顺序排序用点数 割IP值

  1. SELECT ip FROM hostip ORDER BY ip;
  2. SELECT ip FROM hostip ORDER BY SUBSTRING_INDEX(ip,‘.‘,1)+0,SUBSTRING_INDEX(SUBSTRING_INDEX(ip,‘.‘,-3),‘.‘,1)+0,SUBSTRING_INDEX(SUBSTRING_INDEX(ip,‘.‘,-2),‘.‘,1)+0,SUBSTRING_INDEX(ip,‘.‘,-1)+0;
  3. SELECT ip,ip+0 FROM hostip;

自定义排序

  1. SELECT * FROM driver_log ORDER BY FIELD(name,‘Hnry‘,‘Suzi‘,‘Ben‘);

枚举值排序

  1. SELECT day,day+0 FROM weekday ORDER BY day+0;
  2. SELECT day,day+0 FROM weekday ORDER BY day;
  3. SELECT day,day+0 FROM weekday ORDER BY CAST(day as CHAR);
  4. SELECT name FROM color ORDER BY name;
  5. SELECT name FROM color ORDER BY FIELD(name,‘red‘,‘orange‘,‘yellow‘,‘green‘,‘blue‘,‘indigo‘,‘voolet‘);
  6. SELECT * FROM states ORDER BY name;
  7. SELECT COUNT(*) FROM driver_log;
  8. SELECT COUNT(*) FROM states;
  9. SELECT COUNT(*) FROM driver_log WHERE miles > 200;
  10. SELECT COUNT(*) FROM driver_log WHERE name = ‘Suzi‘;
  11. SELECT COUNT(*) FROM states WHERE statehood BETWEEN ‘1800-01-01‘ AND ‘1899-12-31‘;
  12. SELECT COUNT(IF(DAYOFWEEK(trav_date)=7,1,NULL)) AS ‘Saturday trips‘,COUNT(IF(DAYOFWEEK(trav_date)=1,1,NULL)) AS ‘Sunday trips‘ FROM driver_log;

以上是关于查询排序结果的主要内容,如果未能解决你的问题,请参考以下文章

如何根据 ComosDb 中的聚合函数结果对查询结果进行排序?

如何对查询结果进行排序

在SELECT语句中,对查询结果进行排序的子句是啥?能消除重复行的关键字是啥?

如何能实现将oracle的查询结果排序后,只返回第一条记录

如何对Access数据库查询结果进行重新编号排序啊,自动编号的方式也行

在SELECT语句中,对查询结果进行排序的子句是啥?能消除重复行的关键字是啥?