SELECT * FROM driver_log ORDER BY name DESC,trav_date;
表达式进行排序
SELECT t,srcuser,FLOOR((size+1023)/1024) FROM mail WHERE size > 50000 ORDER BY FLOOR((size+1023)/1024);
SELECT t,srcuser,FLOOR((size+1023)/1024) AS kilobytes FROM mail WHERE size > 50000 ORDER BY kilobytes;
查询结果中不包含排序列
SELECT t,srcuser,CONCAT(FLOOR((size+1023)/1024),‘K‘) AS size_in_K FROM mail WHERE size > 50000 ORDER BY size_in_K;
SELECT t,srcuser,CONCAT(FLOOR((size+1023)/1024),‘K‘)AS size_in_K FROM mail WHERE size > 50000 ORDER BY size;
SELECT name,jersey_num FROM roster;
SELECT name,jersey_num FROM roster ORDER BY jersey_num;
SELECT name,jersey_num FROM roster ORDER BY jersey_num+0;
SELECT t,CONCAT(srcuser,‘@‘,srchost) AS sender,size FROM mail WHERE size > 50000 ORDER BY srchost,srcuser;
SELECT last_name,first_name FROM name ORDER BY last_name,first_name;
控制字符串排序大小写区分
SELECT ci_str FROM str_val ORDER BY ci_str;
SELECT cs_str FROM str_val ORDER BY cs_str;
SELECT bin_str FROM str_val ORDER BY bin_str;
SELECT ci_str FROM str_val ORDER BY ci_str COLLATE latin1_general_cs;
SELECT cs_str FROM str_val ORDER BY cs_str COLLATE latin1_swedish_ci;
SELECT bin_str FROM str_val ORDER BY CONVERT(bin_str USING latin1) COLLATE latin1_swedish_ci;
日期排序
SELECT * FROM mail WHERE srcuser = ‘phil‘;
SELECT * FROM mail WHERE srcuser = ‘phil‘ ORDER BY t DESC;
SELECT * FROM mail ORDER BY TIME(t);
SELECT date,description FROM occasion ORDER BY date;
SELECT * FROM mail WHERE srcuser = ‘phil‘;
SELECT * FROM mail WHERE srcuser = ‘phil‘ ORDER BY t DESC;
SELECT DAYNAME(date) AS day,date,description FROM occasion ORDER BY DAYOFWEEK(date);
SELECT DAYNAME(date),date,description FROM occasion ORDER BY MOD(DAYOFWEEK(date)+5,7);
利用列值子字符串排序
SELECT * FROM housewares;
SELECT id,LEFT(id,3) AS category,MID(id,4,5) AS serial,RIGHT(id,2) AS country FROM housewares;
SELECT * FROM housewares ORDER BY LEFT(id,3);
SELECT * FROM housewares ORDER BY MID(id,4,5);
SELECT * FROM housewares ORDER BY RIGHT(id,2),MID(id,4,5);
SELECT category,serial,country,CONCAT(category,serial,country) AS id FROM housewares2 ORDER BY category,country,serial;
SELECT id,LEFT(SUBSTRING(id,4),CHAR_LENGTH(SUBSTRING(id,4)-2)) FROM housewares3;
SELECT id,SUBSTRING(id,4),SUBSTRING(id,4,CHAR_LENGTH(id)-5) FROM housewares3;
SELECT * FROM housewares3 ORDER BY SUBSTRING(id,4,CHAR_LENGTH(id)-5);
SELECT * FROM housewares3 ORDER BY SUBSTRING(id,4)+0;
SELECT *FROM housewares4;
SELECT id,SUBSTRING_INDEX(SUBSTRING_INDEX(id,‘-‘,2),‘-‘,-1) AS segment2,SUBSTRING_INDEX(SUBSTRING_INDEX(id,‘-‘,4),‘-‘,-1) AS segment4 FROM housewares4;
SELECT * FROM housewares4 ORDER BY SUBSTRING_INDEX(SUBSTRING_INDEX(id,‘-‘,2),‘-‘,-1);
SELECT name FROM hostname ORDER BY name;
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;
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值
SELECT ip FROM hostip ORDER BY ip;
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;
SELECT ip,ip+0 FROM hostip;
自定义排序
SELECT * FROM driver_log ORDER BY FIELD(name,‘Hnry‘,‘Suzi‘,‘Ben‘);
枚举值排序
SELECT day,day+0 FROM weekday ORDER BY day+0;
SELECT day,day+0 FROM weekday ORDER BY day;
SELECT day,day+0 FROM weekday ORDER BY CAST(day as CHAR);
SELECT name FROM color ORDER BY name;
SELECT name FROM color ORDER BY FIELD(name,‘red‘,‘orange‘,‘yellow‘,‘green‘,‘blue‘,‘indigo‘,‘voolet‘);
SELECT * FROM states ORDER BY name;
SELECT COUNT(*) FROM driver_log;
SELECT COUNT(*) FROM states;
SELECT COUNT(*) FROM driver_log WHERE miles > 200;
SELECT COUNT(*) FROM driver_log WHERE name = ‘Suzi‘;
SELECT COUNT(*) FROM states WHERE statehood BETWEEN ‘1800-01-01‘ AND ‘1899-12-31‘;
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;