SQL的语言分类
DQL(Data Query Language):数据查询语言 select DML(Data Manipulate Language):数据操作语言 insert 、update、delete DDL(Data Define Languge):数据定义语言 create、drop、alter TCL(Transaction Control Language):事务控制语言 commit、rollback
DQL语言
一、基础查询
进阶1:基础查询
语法:
select 查询列表 from 表名; 类似于:System.out.println(打印东西);
执行顺序:
① from 子句 ② select 子句
特点:
1、查询列表可以是:表中的字段、常量值、表达式、函数 2、查询的结果是一个虚拟的表格
SELECT last_name FROM employees;
SELECT last_name,salary,email FROM employees;
SELECT
`employee_id`,
`first_name`,
`last_name`,
`phone_number`,
`last_name`,
`job_id`,
`phone_number`,
`job_id`,
`salary`,
`commission_pct`,
`manager_id`,
`department_id`,
`hiredate`
FROM employees ;
SELECT * FROM employees;
SELECT 100;
SELECT 'john';
SELECT 100%98;
SELECT DATABASE();
SELECT VERSION();
SELECT 100%98 AS 结果;
SELECT last_name AS 姓,first_name AS 名 FROM employees;
SELECT last_name 姓,first_name 名 FROM employees;
SELECT salary AS "out put" FROM employees;
SELECT DISTINCT department_id FROM employees;
DESC employees;
SHOW COLUMNS FROM employees;
SELECT CONCAT('a','b','c') AS 结果;
SELECT CONCAT(last_name,first_name) AS "姓 名"
FROM employees;
10、【补充】concat函数
功能:拼接字符
select concat(字符1,字符2,字符3,...);
11、【补充】ifnull(表达式1,表达式2)
表达式1:可能为null的字段或表达式
表达式2:如果表达式1为null,则最终结果显示的值
功能:如果表达式1为null,则显示表达式2,否则显示表达式1
select ifnull(commission_pct,0) from employees;
12、【补充】isnull函数
功能:判断某字段或表达式是否为null,如果是,则返回1,否则返回0
二、条件查询
进阶2:条件查询
语法:
select 查询列表
from 表名
where 筛选条件;
执行顺序:
① from子句 ② where子句 ③ select子句
分类:
一、按条件表达式筛选
简单条件运算符:> < = != <> >= <=
SELECT *
FROM employees
WHERE salary>12000;
SELECT
last_name,
department_id
FROM employees
WHERE department_id<>90;
二、按逻辑表达式筛选
逻辑运算符:and or not 作用:用于连接条件表达式 可以用:&& || ! 但不建议使用
- &&和and:两个条件都为true,结果为true,反之为false
- ||或or: 只要有一个条件为true,结果为true,反之为false
- !或not: 如果连接的条件本身为false,结果为true,反之为false
SELECT
last_name,
salary,
commission_pct
FROM employees
WHERE salary>=10000 AND salary<=20000;
SELECT * FROM employees
WHERE NOT(department_id>=90 AND department_id<=110) OR salary>15000;
三、模糊查询
like between and in is null | is not null
1.like
特点: 一般和通配符搭配使用 通配符:
- % 任意多个字符,包含0个字符,支持0-多个
- _ 任意单个字符
SELECT *
FROM employees
WHERE last_name LIKE '%a%';
SELECT
last_name,
salary
FROM
employees
WHERE
last_name LIKE '_ _e_a%';
SELECT
last_name
FROM
employees
WHERE
last_name LIKE '_$_%' ESCAPE '$';
SELECT *
FROM employees
WHERE last_name LIKE '%e';
SELECT *
FROM employees
WHERE last_name LIKE 'e%';
SELECT *
FROM employees
WHERE last_name LIKE '_ _ _ e%';
2.between and
①使用between and 可以提高语句的简洁度 ②包含临界值 ③两个临界值不要调换顺序
SELECT *
FROM employees
WHERE employee_id >= 100 AND employee_id<=120;
SELECT *
FROM employees
WHERE employee_id BETWEEN 100 AND 200;
SELECT `last_name`,`salary`,`salary`*12*(1+IFNULL(`commission_pct`,0)) 年薪
FROM ``employees``
WHERE `salary`*12*(1+IFNULL(`commission_pct`,0)) < 10000 OR `salary`*12*(1+IFNULL(`commission_pct`,0)) > 20000;
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;
3.in
含义: 判断(查询)某字段的值是否属于in(指定)列表中的某一项
- a in(常量值1,常量值2,常量值3,…)
- a not in(常量值1,常量值2,常量值3,…)
特点: ①使用in提高语句简洁度 ②in列表的值类型必须一致或兼容 ③in列表中不支持通配符
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');
4、is null /is not null
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 salary IS 12000;
SELECT last_name,commission_pct
FROM employees
WHERE commission_pct <=>NULL;
SELECT last_name,salary
FROM employees
WHERE salary <=> 12000;
is null pk <=>
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用 <=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
三、排序查询
进阶3:排序查询
语法:
select 查询列表 from 表名 【where 筛选条件】 order by 排序的字段或表达式(排序列表);
执行顺序:
① from ② where ③ select ④ order by
特点:
- asc代表的是升序,可以省略;desc代表的是降序
- order by子句可以支持 单个字段、别名、表达式、函数、多个字段
- order by子句在查询语句的最后面,除了limit子句
1、按单个字段排序
SELECT *
FROM employees
WHERE `employee_id` > 120
ORDER BY salary ASC;
SELECT *
FROM employees
WHERE `employee_id` > 120
ORDER BY salary DESC;
2、添加筛选条件再排序
SELECT *
FROM employees
WHERE department_id>=90
ORDER BY employee_id DESC;
3、按表达式排序
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
WHERE commission_pct IS NOT NULL
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;
SELECT last_name
FROM employees
ORDER BY LENGTH(last_name) DESC;
6、按多个字段排序
SELECT *
FROM employees
ORDER BY salary DESC,employee_id ASC;
7.按列数排序(用的比较少)
SELECT *
FROM `employees`
ORDER BY 2 DESC;
SELECT *
FROM `employees`
ORDER BY `first_name` DESC;
|