一、查询语法
SELECT 字段1,字段2... FROM 表名
WHERE 条件
GROUP BY field
HAVING 筛选
ORDER BY field
LIMIT 限制条数
二、关键字的优先级
重点中的重点:关键字的执行优先级
from
where
group by
having
select
distinct
order by
limit
1.找到表:from
2.拿着where指定的约束条件,去文件/表中取出一条条记录
3.将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组
4.按照select后的字段得到一张新的虚拟表,如果有聚合函数,则将组内数据进行聚合
5.将4的结果过滤:having,如果有聚合函数也是先执行聚合再having过滤
6.查出结果:select
7.去重
8.将结果按条件排序:order by
9.限制结果的显示条数
三、where约束
强调:where是一种约束条件,mysql会拿着where指定的条件去表中取数据,而having则是在取出数据后进行过滤
where字句中可以使用:
1. 比较运算符:> < >= <= <> !=
2. between 80 and 100 值在10到20之间
3. in(80,90,100) 值是10或20或30
4. like 'egon%'
pattern可以是%或_,
%表示任意多字符
_表示一个字符
5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
#1:单条件查询 SELECT name FROM employee WHERE post='sale'; #2:多条件查询 SELECT name,salary FROM employee WHERE post='teacher' AND salary>10000; #3:关键字BETWEEN AND SELECT name,salary FROM employee WHERE salary BETWEEN 10000 AND 20000; SELECT name,salary FROM employee WHERE salary NOT BETWEEN 10000 AND 20000; #4:关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS) SELECT name,post_comment FROM employee WHERE post_comment IS NULL; SELECT name,post_comment FROM employee WHERE post_comment IS NOT NULL; SELECT name,post_comment FROM employee WHERE post_comment=''; 注意''是空字符串,不是null ps: 执行 update employee set post_comment='' where id=2; 再用上条查看,就会有结果了 #5:关键字IN集合查询 SELECT name,salary FROM employee WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ; SELECT name,salary FROM employee WHERE salary IN (3000,3500,4000,9000) ; SELECT name,salary FROM employee WHERE salary NOT IN (3000,3500,4000,9000) ; #6:关键字LIKE模糊查询 通配符’%’ SELECT * FROM employee WHERE name LIKE 'eg%'; 通配符’_’ SELECT * FROM employee WHERE name LIKE 'al__';
表一对一
create table customer( id int primary key auto_increment, name varchar(50), qq int, phine int ); insert into customer(name,qq,phine) values('egon','251575084','1779422095'), ('alex','251575089','1779422558'); create table student( id int primary key auto_increment, course varchar(20), c_id int, foreign key(c_id) references customer(id) on delete cascade on update cascade ); insert into student(course,c_id) values ('python','5');
表一对多
create table press( id int primary key auto_increment, name varchar(20) ); create table book2( id int primary key auto_increment, name varchar(20), press_id int not null, foreign key(press_id) references press(id) on delete cascade on update cascade ); insert into press(name) values ('人民文学出版社'), ('人民邮电出版社'), ('科学出版社'); insert into book2(name,press_id) values ('和平',1), ('世界十大名著',2), ('世界上的另一个你',2), ('人性弱点',3), ('爆笑校园',2), ('傲慢与偏见',3);
多对一
create table user( id int primary key auto_increment, name varchar(10) ); create table password( id int primary key auto_increment, user_id int , password varchar(20), foreign key(user_id) references password(id) on update cascade on delete cascade ); insert into user(name) values ('egon1'), ('egon2'), ('egon3'), ('egon4'), ('egon5'), ('egon6'); insert into password(user_id,password) values (3,'alex3714'), (5,'alex371a');
多对多
create table book( id int primary key auto_increment, name varchar(10), price int ); create table author( id int primary key auto_increment, name varchar(10) ); create table author2book( id int primary key auto_increment, book_id int , author_id int, foreign key(book_id) references author2book(id) on delect cascade on delect cascade, foreign key(author_id) references author2book(id) on delect cascade on delect cascade, unique(book_id,author_id) );