MySQL之排序查询(DQL)

ORDER BY

介绍:

  使用ORDER BY子句排序:

    ASC(ascend):升序

    DESC(descend):降序

  ORDER BY子句在SELECT语句的结尾。

语法:

  SELECT 要查询的东西 FROMWHERE 条件 ORDER BY 排序的字段|表达式|函数|别名 【asc|desc

升序排序:

SELECT last_name,job_id,department_id,hiredate FROM employees ORDER BY hiredate;

降序排序:

SELECT last_name,job_id,department_id,hiredate FROM employees ORDER BY hiredate DESC ;

按别名排序:

SELECT employee_id,last_name,salary*12 annsal FROM employees ORDER BY annsal;

多个列排序:

SELECT last_name,department_id,salary FROM employees ORDER BY department_id, salary DESC;

案例讲解

#1.按单个字段排序
SELECT * FROM employees ORDER BY salary DESC;


#2.添加筛选条件再排序
#案例:查询部门编号>=90的员工信息,并按员工编号降序
SELECT * FROM employees WHERE department_id>=90 ORDER BY employee_id DESC;


#3.按表达式排序
#案例:查询员工信息按年薪降序
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) FROM employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;


#4.按别名排序
#案例:查询员工信息按年薪升序
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM employees ORDER BY 年薪 ASC;


#5.按函数排序
#案例:查询员工名,并且按名字的长度降序
SELECT LENGTH(last_name),last_name FROM employees ORDER BY LENGTH(last_name) DESC;


#6.按多个字段排序
#案例:查询员工信息,要求先按工资降序,再按employee_id升序
SELECT * FROM employees ORDER BY salary DESC,employee_id ASC;


#7.查询员工的姓名和部门号和年薪,按年薪降序、按姓名升序
SELECT last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM employees ORDER BY 年薪 DESC,last_name ASC;


#8.选择工资不在8000到17000的员工的姓名和工资,按工资降序
SELECT last_name,salary FROM employees WHERE salary NOT BETWEEN 8000 AND 17000 ORDER BY salary DESC;


#9.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT *,LENGTH(email) FROM employees WHERE email LIKE %e% ORDER BY LENGTH(email) DESC,department_id ASC;

测试数据

#员工表
CREATE TABLE `employees` (
  `employee_id` int(6) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(20) DEFAULT NULL,
  `last_name` varchar(25) DEFAULT NULL,
  `email` varchar(25) DEFAULT NULL,
  `phone_number` varchar(20) DEFAULT NULL,
  `job_id` varchar(10) DEFAULT NULL,
  `salary` double(10,2) DEFAULT NULL,
  `commission_pct` double(4,2) DEFAULT NULL,
  `manager_id` int(6) DEFAULT NULL,
  `department_id` int(4) DEFAULT NULL,
  `hiredate` datetime DEFAULT NULL,
  PRIMARY KEY (`employee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#员工数据
INSERT INTO employees VALUES (100, Steven, K_ing, SKING, 515.123.4567, AD_PRES, 24000.00, null, null, 90, 1992-04-03 00:00:00);
INSERT INTO employees VALUES (101, Neena, Kochhar, NKOCHHAR, 515.123.4568, AD_VP, 17000.00, null, 100, 90, 1992-04-03 00:00:00);
INSERT INTO employees VALUES (102, Lex, De Haan, LDEHAAN, 515.123.4569, AD_VP, 17000.00, null, 100, 90, 1992-04-03 00:00:00);
INSERT INTO employees VALUES (103, Alexander, Hunold, AHUNOLD, 590.423.4567, IT_PROG, 9000.00, null, 102, 60, 1992-04-03 00:00:00);
INSERT INTO employees VALUES (104, Bruce, Ernst, BERNST, 590.423.4568, IT_PROG, 6000.00, null, 103, 60, 1992-04-03 00:00:00);
INSERT INTO employees VALUES (105, David, Austin, DAUSTIN, 590.423.4569, IT_PROG, 4800.00, null, 103, 60, 1998-03-03 00:00:00);
INSERT INTO employees VALUES (106, Valli, Pataballa, VPATABAL, 590.423.4560, IT_PROG, 4800.00, null, 103, 60, 1998-03-03 00:00:00);
INSERT INTO employees VALUES (107, Diana, Lorentz, DLORENTZ, 590.423.5567, IT_PROG, 4200.00, null, 103, 60, 1998-03-03 00:00:00);
INSERT INTO employees VALUES (108, Nancy, Greenberg, NGREENBE, 515.124.4569, FI_MGR, 12000.00, null, 101, 100, 1998-03-03 00:00:00);
INSERT INTO employees VALUES (109, Daniel, Faviet, DFAVIET, 515.124.4169, FI_ACCOUNT, 9000.00, null, 108, 100, 1998-03-03 00:00:00);
INSERT INTO employees VALUES (110, John, Chen, JCHEN, 515.124.4269, FI_ACCOUNT, 8200.00, null, 108, 100, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (111, Ismael, Sciarra, ISCIARRA, 515.124.4369, FI_ACCOUNT, 7700.00, null, 108, 100, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (112, Jose Manuel, Urman, JMURMAN, 515.124.4469, FI_ACCOUNT, 7800.00, null, 108, 100, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (113, Luis, Popp, LPOPP, 515.124.4567, FI_ACCOUNT, 6900.00, null, 108, 100, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (114, Den, Raphaely, DRAPHEAL, 515.127.4561, PU_MAN, 11000.00, null, 100, 30, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (115, Alexander, Khoo, AKHOO, 515.127.4562, PU_CLERK, 3100.00, null, 114, 30, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (116, Shelli, Baida, SBAIDA, 515.127.4563, PU_CLERK, 2900.00, null, 114, 30, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (117, Sigal, Tobias, STOBIAS, 515.127.4564, PU_CLERK, 2800.00, null, 114, 30, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (118, Guy, Himuro, GHIMURO, 515.127.4565, PU_CLERK, 2600.00, null, 114, 30, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (119, Karen, Colmenares, KCOLMENA, 515.127.4566, PU_CLERK, 2500.00, null, 114, 30, 2000-09-09 00:00:00);
INSERT INTO employees VALUES (120, Matthew, Weiss, MWEISS, 650.123.1234, ST_MAN, 8000.00, null, 100, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (121, Adam, Fripp, AFRIPP, 650.123.2234, ST_MAN, 8200.00, null, 100, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (122, Payam, Kaufling, PKAUFLIN, 650.123.3234, ST_MAN, 7900.00, null, 100, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (123, Shanta, Vollman, SVOLLMAN, 650.123.4234, ST_MAN, 6500.00, null, 100, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (124, Kevin, Mourgos, KMOURGOS, 650.123.5234, ST_MAN, 5800.00, null, 100, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (125, Julia, Nayer, JNAYER, 650.124.1214, ST_CLERK, 3200.00, null, 120, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (126, Irene, Mikkilineni, IMIKKILI, 650.124.1224, ST_CLERK, 2700.00, null, 120, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (127, James, Landry, JLANDRY, 650.124.1334, ST_CLERK, 2400.00, null, 120, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (128, Steven, Markle, SMARKLE, 650.124.1434, ST_CLERK, 2200.00, null, 120, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (129, Laura, Bissot, LBISSOT, 650.124.5234, ST_CLERK, 3300.00, null, 121, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (130, Mozhe, Atkinson, MATKINSO, 650.124.6234, ST_CLERK, 2800.00, null, 121, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (131, James, Marlow, JAMRLOW, 650.124.7234, ST_CLERK, 2500.00, null, 121, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (132, TJ, Olson, TJOLSON, 650.124.8234, ST_CLERK, 2100.00, null, 121, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (133, Jason, Mallin, JMALLIN, 650.127.1934, ST_CLERK, 3300.00, null, 122, 50, 2004-02-06 00:00:00);
INSERT INTO employees VALUES (134, Michael, Rogers, MROGERS, 650.127.1834, ST_CLERK, 2900.00, null, 122, 50, 2002-12-23 00:00:00);
INSERT INTO employees VALUES (135, Ki, Gee, KGEE, 650.127.1734, ST_CLERK, 2400.00, null, 122, 50, 2002-12-23 00:00:00);
INSERT INTO employees VALUES (136, Hazel, Philtanker, HPHILTAN, 650.127.1634, ST_CLERK, 2200.00, null, 122, 50, 2002-12-23 00:00:00);
INSERT INTO employees VALUES (137, Renske, Ladwig, RLADWIG, 650.121.1234, ST_CLERK, 3600.00, null, 123, 50, 2002-12-23 00:00:00);
INSERT INTO employees VALUES (138, Stephen, Stiles, SSTILES, 650.121.2034, ST_CLERK, 3200.00, null, 123, 50, 2002-12-23 00:00:00);
INSERT INTO employees VALUES (139, John, Seo, JSEO, 650.121.2019, ST_CLERK, 2700.00, null, 123, 50, 2002-12-23 00:00:00);
INSERT INTO employees VALUES (140, Joshua, Patel, JPATEL, 650.121.1834, ST_CLERK, 2500.00, null, 123, 50, 2002-12-23 00:00:00);
INSERT INTO employees VALUES (171, William, Smith, WSMITH, 011.44.1343.629268, SA_REP, 7400.00, 0.15, 148, 80, 2014-03-05 00:00:00);
INSERT INTO employees VALUES (172, Elizabeth, Bates, EBATES, 011.44.1343.529268, SA_REP, 7300.00, 0.15, 148, 80, 2014-03-05 00:00:00);
INSERT INTO employees VALUES (173, Sundita, Kumar, SKUMAR, 011.44.1343.329268, SA_REP, 6100.00, 0.10, 148, 80, 2014-03-05 00:00:00);
INSERT INTO employees VALUES (201, Michael, Hartstein, MHARTSTE, 515.123.5555, MK_MAN, 13000.00, null, 100, 20, 2016-03-03 00:00:00);
INSERT INTO employees VALUES (202, Pat, Fay, PFAY, 603.123.6666, MK_REP, 6000.00, null, 201, 20, 2016-03-03 00:00:00);
INSERT INTO employees VALUES (203, Susan, Mavris, SMAVRIS, 515.123.7777, HR_REP, 6500.00, null, 101, 40, 2016-03-03 00:00:00);
INSERT INTO employees VALUES (204, Hermann, Baer, HBAER, 515.123.8888, PR_REP, 10000.00, null, 101, 70, 2016-03-03 00:00:00);
INSERT INTO employees VALUES (205, Shelley, Higgins, SHIGGINS, 515.123.8080, AC_MGR, 12000.00, null, 101, 110, 2016-03-03 00:00:00);
INSERT INTO employees VALUES (206, William, Gietz, WGIETZ, 515.123.8181, AC_ACCOUNT, 8300.00, null, 205, 110, 2016-03-03 00:00:00);

MySQL之排序查询(DQL)

上一篇:sqlalchemy基本增删改查


下一篇:通过v$sqlarea,v$sql查询最占用资源的查询