目录
INSERT INTO语句: 1
UPDATE语句: 1
DELETE语句: 1
SELECT语句: 2
WHERE子句: 2
ORDER BY子句: 2
DISTINCT 3
聚合函数: 3
GROUP BY分组: 3
子查询: 4
join连接: 5
cross join,交叉连接: 5
inner join,内连接: 5
outer join,外连接: 5
自连接: 6
关系操作:
在关系型DB中,关系就是二维表;
关系操作就是对象的操作;
selection,选择,又称限制,是从关系中选择出满足给定条件的元组,横向;
projection,投影,在关系上投影,就是选择出若干属性列组成新的关系,纵向,按字段;
join连接,将不同的两个关系连接成一个关系;
DML:
CRUD;
INSERT INTO语句:
insert into table_name (col_name,...) values(value1,...);
向表中插入一条数据,自增字段、缺省值字段、可为空字段(可不写);
insert into table_name SELECT ...;
insert into table_name (col_name,...) values(value1,...) on duplicate key update col_name1=value1,...; #生产中经常用,如果主键冲突、唯一键冲突就执行update后的设置,即主键不在则新增记录,主键在则更新部分字段;在已有数据情形下的解决方案;
insert ignore into如果主键冲突、唯一键冲突,就忽略错误,返回一个警告;
UPDATE语句:
update [ignore] tal_name set col_name1=expr1 [,col_name2=expr2...] where where_definition;
DELETE语句:
delete [ignore] from tal_name where where_definition;
SELECT语句:
select
[distince]
[
from table_references
[where where_definition]
[group by {col_name|expr|position} [asc|desc],... [with rollup]]
[having where_definition]
[order by {col_name|expr|position} [asc|desc],...]
[limit {[offset,] row_count|row_count offset OFFSET}]
{for update|lock in share mode]
]
for update,排他锁,写锁定;
lock in share mode,共享锁;
sql语句执行过程:
先from-->再where-->再分组group by-->再聚合-->再过滤having-->最后order by;
复杂查询,用工具,“查询创建工具”,在某一张表中打勾,托拽,再改语句;
WHERE子句:
=,相当于py的==;
<>,不等于;
>,<,>=,<=
between,在某个范围之内,between a and b,等价于py的[a,b]闭区间;
like,字符串模式匹配,%表示任意多个字符,_表示一个字符;
in,指针对某个列的多个可能值;
and,与;
or,或;如果很多表达式要使用and、or计算逻辑表达的值的时候,由于有结合律的问题,建议使用小括号来避免产生错误;
ORDER BY子句:
对查询结果进行排序,ASC升序,DESC降序,默认升序;
此句一般放在最后;
DISTINCT
返回不重复的记录;
聚合函数:
count(expr),返回记录中记录的数目,如果指定列,则返回非null值的行数;
count(DISTINCT expr [,expr...]),返回不重复的非nul值的行数;
sum([distinct] expr),求和,返回不同值求和;
avg([distinct] expr),返回平均值,返回不同值的平均值;
MIN(expr)
MAX(expr)
GROUP BY分组:
HAVING子句:
过滤,对分组结果进行过滤;
同WHERE,但WHERE在HAVING之前;
例:
select 1 ;
select 1 as a;
select emp_no,concat(first_name,last_name) as name from employees;合并
select emp_no,last_name from employees limit 5;返回5条
select emp_no,last_name from employees limit 5 offset 2; 返回5条,偏移2条;很有用,用于分页,如每页20行,第2页跳过20行,第3页跳过40行;
select emp_no,last_name from employees limit 2,5;同limit 5 offset 2,返回5条偏移2条
select * from employees where emp_no < 10015 and last_name like 'P%';
select * from employees where emp_no between 10010 and 10015 and last_name like 'P%';
select * from employees where emp_no in (10001,10002,10010) order by emp_no desc;
注:查询编辑器中,选中要查询的语句,右键“运行已选择的”
select * from employees order by emp_no desc,gender desc;优先,如果要两个都按降序,要分别写上
select distinct emp_no,salary from salaries;经常这么做
select count(*) from salaries; 最常用到,count()里要么PK,要么*,同样效果
select count(emp_no) from salaries;
select sum(salary) from salaries;
select sum(salary),count(*),avg(salary) from salaries;
select sum(salary) f1,count(*) f2,avg(salary) f3,min(salary) f4,max(salary) f5 from salaries;
select sum(salary) f1,count(*) f2,avg(salary) f3,min(salary) f4,max(salary) f5 from salaries where emp_no > 10003;优先级高
select emp_no,sum(salary) from salaries group by emp_no having emp_no > 10003;
select emp_no,count(*),sum(salary),avg(salary),min(salary) from salaries group by emp_no having emp_no > 10002;分组统计数据
子查询:
查询语句可嵌套,内部查询就是子查询;
子查询必须在一组小括号中;
子查询中不能使用order by排序(无用功,是给别人用的);
少用子查询,不要滥用子查询,会有效率问题,推荐用join;
select * from employees where emp_no in (select emp_no from employees where emp_no > 10015) order by emp_no desc;无用语句,只是用来举例子查询
常见应用场景:
select * from employees where emp_no in (select distinct emp_no from salaries); 推荐用join
select emp.emp_no,emp.first_name,gender from (select * from employees where emp_no > 10015) as emp where emp.emp_no < 10019 order by emp.emp_no desc; 没写成emp.gender,如果有重复,先找到谁就直接显示了;此句通常为view,在此处为临时表子句中显示的数据能少就少
join连接:
cross join,交叉连接:
MySQL中,cross join语法上等同于inner join;
全部交叉,笛卡尔乘积,最不希望看到的;
例:
select * frm employees cross join salaries;
inner join,内连接:
可省略为join;
等值连接,只选某些field相等的元组(行),使用on限定关联的结果;
自然连接,特殊的等值连接,会去掉重复的列;
例:
select * from employees inner join salaries on employees.emp_no = salaries.emp_no;等值连接
select employees.*,salaries.salary from employees inner join salaries on employees.emp_no = salaries.emp_no;
select * from employees natural join salaries;自然连接,等值连接,将相同的列隐掉只显示一次,很少用,一般自己用inner join将指定的字段列出(投影)
outer join,外连接:
可省略为join;
分为左外连接left outer join(左连接)、右外连接right outer join(右连接)、全外连接;
看表的数据的方向,谁是主表,谁的所有数据都显示;
例:
select * from employees left join salaries on employees.emp_no = salaries.emp_no;左连接
select * from employees right join salaries on employees.emp_no = salaries.emp_no;右连接
select * from salaries right join employees on employees.emp_no = salaries.emp_no;
自连接:
自己和自己连接;
设计树时,乘积树型,不会建多张表,用一张表,用自连接;
例:
select manager.* from employees as manager,employees as worker where manager.emp_no = worker.mgr_no and worker.emp_no = 1;