DQL(data query language)(数据 查询 语言)
SQL(s=select)(选择 查询 语言)
DML(data manipulation language)(数据 操作 语言)
DDL(data define language)(数据定义语言)
TCL(transaction control language)(事务控制语言)
特点:查询的结果集 是一个虚拟表(打印内容)
select后面跟的查询列表,可以有多个部分组成,中间用逗号隔开:select 字段1,字段2,表达式 from 表;
执行顺序
① from子句
② select子句
查询列表可以是:字段、表达式、常量、函数等
USE myemployees;
一、查询常量
SELECT 100 ;
二、查询表达式
SELECT 100%3;
三、查询单个字段
SELECT last_name
FROM employees
;
四、查询多个字段
SELECT last_name
,email
,employee_id
FROM employees;
五、查询所有字段
SELECT * FROM employees
;
SELECT DATABASE();
SELECT VERSION();
SELECT USER();
方式一:使用as关键字
SELECT USER() AS 用户名;
SELECT USER() AS “用户名”;
SELECT USER() AS ‘用户名’;
SELECT last_name AS “姓 名” FROM employees;
方式二:使用空格
SELECT USER() 用户名;
SELECT USER() “用户名”;
SELECT USER() ‘用户名’;
SELECT last_name “姓 名” FROM employees;
mysql> select last_name as 'xing ming' from employees;
+-------------+
| xing ming |
+-------------+
| K_ing |
| Kochhar |
| De Haan |
| Hunold |
-需求:查询 first_name 和last_name 拼接成的全名,最终起别名为:姓 名
方案1:使用+ pass×
SELECT first_name+last_name AS “姓 名”
FROM employees;
方案2:使用concat拼接函数
SELECT CONCAT(first_name,last_name) AS “姓 名”
FROM employees;
mysql> select concat(first_name,last_name) as 'xing ming' from employees;
+------------------+
| xing ming |
+------------------+
| StevenK_ing |
| NeenaKochhar |
| LexDe Haan |
| AlexanderHunold |
| BruceErnst |
| DavidAustin |
| ValliPataballa |
| DianaLorentz |
| NancyGreenberg |
mysql> select department_id from employees; #有重复
+---------------+
| department_id |
+---------------+
| NULL |
| 10 |
| 20 |
| 20 |
| 30 |
| 30 |
| 30 |
| 30 |
| 30 |
| 30 |
distinct adj. 明显的;独特的;清楚的;有区别的
查询员工涉及到的部门编号有哪些
SELECT DISTINCT department_id FROM employees;
mysql> select distinct department_id from employees; #无重复
+---------------+
| department_id |
+---------------+
| NULL |
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
| 60 |
| 70 |
| 80 |
| 90 |
| 100 |
| 110 |
+---------------+
12 rows in set (0.00 sec)
DESC employees;
列 columns
SHOW COLUMNS FROM employees;
select 查询列表
from 表名
where 筛选条件;
执行顺序:
①from子句
②where子句
③select子句
select last_name,first_name from employees where salary>20000;
关系运算符:> < >= <= = <> (或 != )(不等于)
mysql> select * #要查的东西
-> from employees #表名
-> where department_id <> 100; #条件
+-------------+-------------+-------------+----------+--------------------+------------+----------+----------------+------------+---------------+---------------------+
| employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
+-------------+-------------+-------------+----------+--------------------+------------+----------+----------------+------------+---------------+---------------------+
| 100 | Steven | K_ing | SKING | 515.123.4567 | AD_PRES | 24000.00 | NULL | NULL | 90 | 1992-04-03 00:00:00 |
| 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
| 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
| 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | IT_PROG | 9000.00 | NULL | 102 | 60 | 1992-04-03 00:00:00 |
| 104 | Bruce | Ernst | BERNST | 590.423.4568 | IT_PROG | 6000.00 | NULL | 103 | 60 | 1992-04-03 00:00:00 |
| 105 | David | Austin | DAUSTIN | 590.423.4569 | IT_PROG | 4800.00 | NULL | 103 | 60 | 1998-03-03 00:00:00 |
| 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | IT_PROG | 4800.00 | NULL | 103 | 60 | 1998-03-03 00:00:00 |
| 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | IT_PROG | 4200.00 | NULL | 103 | 60 | 1998-03-03 00:00:00 |
| 114 | Den | Raphaely | DRAPHEAL | 515.127.4561 | PU_MAN | 11000.00 | NULL | 100 | 30 | 2000-09-09 00:00:00 |
| 115 | Alexander | Khoo | AKHOO | 515.127.4562 | PU_CLERK | 3100.00 | NULL | 114 | 30 | 2000-09-09 00:00:00 |
| 116 | Shelli | Baida | SBAIDA | 515.127.4563 | PU_CLERK | 2900.00 | NULL | 114 | 30 | 2000-09-09 00:00:00 |
| 117 | Sigal | Tobias | STOBIAS | 515.127.4564 | PU_CLERK | 2800.00 | NULL | 114 | 30 | 2000-09-09 00:00:00 |
| 118 | Guy | Himuro | GHIMURO | 515.127.4565 | PU_CLERK | 2600.00 | NULL |
案例2:查询工资<15000的姓名、工资
mysql> select last_name,salary #要查的东西
-> from employees #表名
-> where salary<15000; #筛选条件
+-------------+----------+
| last_name | salary |
+-------------+----------+
| Hunold | 9000.00 |
| Ernst | 6000.00 |
| Austin | 4800.00 |
逻辑运算符:and or not 也可以使用&& || !
案例:查询奖金率>0.03 或者 员工编号在60-110之间的员工信息
mysql> select *
-> from employees
-> where commission_pct >0.03 or(employee_id >=60 and employee_id<=110);
+-------------+-------------+------------+----------+--------------------+------------+----------+----------------+------------+---------------+---------------------+
| employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
+-------------+-------------+------------+----------+--------------------+------------+----------+----------------+------------+---------------+---------------------+
| 100 | Steven | K_ing | SKING | 515.123.4567 | AD_PRES | 24000.00 | NULL | NULL | 90 | 1992-04-03 00:00:00 |
| 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
| 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
| 103 | Alexander
功能:一般和通配符搭配使用,对字符型数据进行部分匹配查询
常见的通配符:
_ 任意单个字符
% 任意多个字符,支持0-多个
like/not like
案例1:查询姓名中包含字符a的员工信息
mysql> select *
-> from employees
-> where last_name like '%a%';
+-------------+-------------+------------+----------+--------------------+------------+----------+----------------+------------+---------------+---------------------+
| employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
+-------------+-------------+------------+----------+--------------------+------------+----------+----------------+------------+---------------+---------------------+
| 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
| 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
| 105 | David | Austin | DAUSTIN | 590.423.4569 | IT_PROG | 4800.00 | NULL | 103 |
案例2:查询姓名中包含第三个字符为x的员工信息
mysql> select *
-> from employees
-> where last_name like '__x%';
+-------------+------------+-----------+-------+--------------------+--------+---------+----------------+------------+---------------+---------------------+
| employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
+-------------+------------+-----------+-------+--------------------+--------+---------+----------------+------------+---------------+---------------------+
| 170 | Tayler | Fox | TFOX | 011.44.1343.729268 | SA_REP | 9600.00 | 0.20 | 148 | 80 | 2014-03-05 00:00:00 |
+-------------+------------+-----------+-------+--------------------+--------+---------+----------------+------------+---------------+---------------------+
1 row in set (0.00 sec)
案例3:查询姓名中包含第二个字符为_的员工信息
mysql> select *
-> from employees
-> where last_name like '_\_%';
+-------------+------------+-----------+-------+--------------------+---------+----------+----------------+------------+---------------+---------------------+
| employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
+-------------+------------+-----------+-------+--------------------+---------+----------+----------------+------------+---------------+---------------------+
| 100 | Steven | K_ing | SKING | 515.123.4567 | AD_PRES | 24000.00 | NULL | NULL | 90 | 1992-04-03 00:00:00 |
| 156 | Janette | K_ing | JKING | 011.44.1345.429268 | SA_REP | 10000.00 | 0.35 | 146 | 80 | 2014-03-05 00:00:00 |
+-------------+------------+-----------+-------+--------------------+---------+----------+----------------+------------+---------------+---------------------+
2 rows in set (0.00 sec)
SELECT *
FROM employees
WHERE last_name LIKE '_$_%' ESCAPE '$'; #将¥定义为转义字符
escape
vt. 逃避,避开,避免;被忘掉;被忽视
vi. 逃脱;避开;溜走;
查询某字段的值是否属于指定的列表之内
a in(常量值1,常量值2,常量值3,…)
a not in(常量值1,常量值2,常量值3,…)
in/not in
案例1:查询部门编号是30/50/90的员工名、部门编号
#方式1:
SELECT last_name,department_id
FROM employees
WHERE department_id IN(30,50,90);
#方式2:
SELECT last_name,department_id
FROM employees
WHERE department_id = 30
OR department_id = 50
OR department_id = 90;
案例2:查询工种编号不是SH_CLERK或IT_PROG的员工信息
#方式1:
SELECT *
FROM employees
WHERE job_id NOT IN('SH_CLERK','IT_PROG');
#方式2:
SELECT *
FROM employees
WHERE NOT(job_id ='SH_CLERK'
OR job_id = 'IT_PROG');
mysql> select *
-> from employees
-> where not(job_id ='SH_CLERK'
-> or job_id ='IT_PROG');
+-------------+-------------+-------------+----------+--------------------+------------+----------+----------------+------------+---------------+---------------------+
| employee_id | first_name | last_name | email | phone_number | job_id | salary | commission_pct | manager_id | department_id | hiredate |
+-------------+-------------+-------------+----------+--------------------+------------+----------+----------------+------------+---------------+---------------------+
| 100 | Steven | K_ing | SKING | 515.123.4567 | AD_PRES | 24000.00 | NULL | NULL | 90 | 1992-04-03 00:00:00 |
| 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | AD_VP | 17000.00 | NULL | 100 | 90 | 1992-04-03 00:00:00 |
| 10
判断某个字段的值是否介于xx之间
between and/not between and
案例1:查询部门编号是30-90之间的部门编号、员工姓名
#方式1:
SELECT department_id,last_name
FROM employees
WHERE department_id BETWEEN 30 AND 90;
#方式2:
SELECT department_id,last_name
FROM employees
WHERE department_id>=30 AND department_id<=90;
案例2:查询年薪不是100000-200000之间的员工姓名、工资、年薪
SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
WHERE salary*12*(1+IFNULL(commission_pct,0))<100000 OR salary*12*(1+IFNULL(commission_pct,0))>200000;
SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
WHERE salary*12*(1+IFNULL(commission_pct,0)) NOT BETWEEN 100000 AND 200000;
mysql> SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0))
-> from employees
-> WHERE salary*12*(1+IFNULL(commission_pct,0)) NOT BETWEEN 100000 AND 200000;
+-------------+----------+----------------------------------------+
| last_name | salary | salary*12*(1+IFNULL(commission_pct,0)) |
+-------------+----------+----------------------------------------+
| K_ing | 24000.00 | 288000.00 |
| Kochhar | 17000.00 | 204000.00 |
| De Haan | 17000.00 | 204000.00 |
| Ernst | 6000.00 | 72000.00 |
| Austin | 4800.00 | 57600.00 |
案例1:查询没有奖金的员工信息
SELECT *
FROM employees
WHERE commission_pct IS NULL;
案例2:查询有奖金的员工信息
SELECT *
FROM employees
WHERE commission_pct IS NOT NULL;
= 只能判断普通的内容
IS 只能判断NULL值
<=> 安全等于,既能判断普通内容,又能判断NULL值
SELECT *
FROM employees
WHERE salary <=> 10000;
SELECT *
FROM employees
WHERE commission_pct <=> NULL;