码迷,mamicode.com
首页 > 数据库 > 详细

MySQL之条件查询(DQL)

时间:2020-07-17 11:28:37      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:concat   ada   san   逻辑运算   选择   and   als   utf8   通配符   

条件查询

介绍:

  根据条件过滤原始表的数据,查询到想要的数据。

语法:

  select 要查询的字段|表达式|常量值|函数 fromwhere 条件;

WHERE

介绍:

  使用WHERE子句,将不满足条件的行过滤掉,WHERE子句紧随FROM子句。

示例:

SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90 ;

比较运算

介绍:

  =:等于

  >:大于

  >=:大于、等于

  <:小于

  <=:小于、等于

  <>:不等于 (也可以是!=)

示例:

SELECT last_name, salary FROM employees WHERE salary <= 3000;

BETWEEN

介绍:

  使用BETWEEN运算来显示在一个区间内的值。

特点:

  1.使用between and可以提高语句的简洁度。

  2.包含临界值。

  3.两个临界值不要调换顺序。

示例:

SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500;

IN

介绍:

  判断某字段的值是否属于in列表中的某一项。

特点:

  1.使用in提高语句简洁度。

  2.in列表的值类型必须一致或兼容。

  3.in列表中不支持通配符。

示例:

SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201);

LIKE

介绍:

  1.使用LIKE运算选择类似的值。

  2.一般和通配符搭配使用,选择条件可以包含字符或数字:

    %代表零个或多个字符(任意个字符)

    _代表一个字符

  3.%_可以同时使用。

示例:

SELECT first_name FROM employees WHERE first_name LIKE S%;

SELECT last_name FROM employees WHERE last_name LIKE _o%;

SELECT last_name FROM employees WHERE last_name LIKE CONCAT(%,o,%);

NULL

介绍:

  =<>不能用于判断null值,使用IS (NOT) NULL判断空值。

特点:

  IS NULL:仅仅可以判断NULL值,可读性较高,建议使用。
  安全等于<=>:既可以判断NULL值,又可以判断普通的数值,可读性较低。

示例:

SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL;

逻辑运算AND

介绍(&&):

  逻辑并,两个条件如果同时成立,结果为true,否则为false

示例:

SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >=10000 AND job_id LIKE %MAN%;

逻辑运算OR

介绍(||):

  逻辑或,两个条件只要有一个成立,结果为true,否则为false

示例:

SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 OR job_id LIKE %MAN%;

逻辑运算NOT

介绍:

  逻辑否,如果条件成立,则not后为false,否则为true

示例:

SELECT last_name, job_id FROM employees WHERE job_id NOT IN (IT_PROG, ST_CLERK, SA_REP);

案例讲解

#案例:查询工资>12000的员工信息
SELECT * FROM employees WHERE salary>12000;


#案例:查询部门编号不等于90号的员工名和部门编号
SELECT last_name,department_id FROM employees WHERE department_id<>90;


#案例:查询工资在10000到20000之间的员工名、工资以及奖金
SELECT last_name,salary,commission_pct FROM employees WHERE salary>=10000 AND salary<=20000;


#案例:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
SELECT * FROM employees WHERE NOT(department_id>=90 AND  department_id<=110) OR salary>15000;


#案例:查询员工名中包含字符a的员工信息
SELECT * FROM employees WHERE last_name LIKE %a%;


#案例:查询员工名中第三个字符为n,第五个字符为l的员工名和工资
SELECT last_name,salary FROM employees WHERE last_name LIKE __n_l%;


#案例:查询员工名中第二个字符为_的员工名
SELECT last_name FROM employees WHERE last_name LIKE _$_% ESCAPE $;


#案例:查询员工编号在100到120之间的员工信息
SELECT * FROM employees WHERE employee_id >= 100 AND employee_id<=120;
SELECT * FROM employees WHERE employee_id BETWEEN 100 AND 120;


#案例:查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
SELECT last_name,job_id FROM employees WHERE job_id = IT_PROT OR job_id = AD_VP OR JOB_ID =AD_PRES;
SELECT last_name,job_id FROM employees WHERE job_id IN( IT_PROT ,AD_VP,AD_PRES);


#案例:查询没有奖金的员工名和奖金率
SELECT last_name,commission_pct FROM employees WHERE commission_pct IS NULL;


#案例:查询有奖金的员工名和奖金率
SELECT last_name,commission_pct FROM employees WHERE commission_pct IS NOT NULL;


#案例:查询没有奖金的员工名和奖金率
SELECT last_name,commission_pct FROM employees WHERE commission_pct <=>NULL;


#案例:查询工资为12000的员工信息
SELECT last_name,salary FROM employees WHERE salary <=> 12000;

测试数据

#员工表
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)

标签:concat   ada   san   逻辑运算   选择   and   als   utf8   通配符   

原文地址:https://www.cnblogs.com/hfl1996/p/13323968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!