1.模糊查询
select * from 表名 where 字段 like ‘%w%’;
select * from grade where name like '%java%'; %号中间写查询数据的内容 这个用的比较多
select * from grade where name like 'java%'; 百分号写在后面的意思是 查询java开头的数据
select * from grade where name like '%java'; 百分号写在前面的意思是 查询java结尾的数据
select * from 表名 where 字段 like ‘李_’;
select * from grade where name like 'java_'; 一个下划线的意思是查询这条数据后面只有一个字符的数据 而且是必须有一个字符
2.去除重复数据
select distinct 字段1 from 表名;
select distinct tid from grade; 去除tid这个字段下重复的数据 主要是distinct这个词
3.排序
select *from 表名 order by 列名 asc(asc升序默认;desc降序) order by 这个是排序的关键字
select *from score order by score; 这个是升序排序 没写就是默认的asc
select *from score order by score desc;这个是降序排序 是desc
4.聚合函数查询
select 关键词(列名) from 表名
select max(score) from score;这个是找最大值
select min(score) from score;这个是找最小值
select avg(score) from score;这个是找平均值
select sum(score) from score;这个是值得求和
select count(*) from score;查询一共有多少条数据就是统计行数 这个关键词里面必须有参数 所以写了个通配符*号
5.分组查询
查询 最大值 最小值... 加tid这一列 表名 根据tid来分组
select max(score),min(score),avg(score),sum(score),count(*),tid from score group by tid; 关键词是 group by
分组后筛选使用
select tid from score group by tid having count(*)>3; 这个是分组查询后在用having 来限制查询条件的输出
select tid from score where score=90 group by tid having count(*); 如果在加where限制条件的输出 需要加到group by前面
6.分页查询
0是从哪一行开始 2是显示几行 页码减一然后乘以行数
select * from score limit 0,2;
7.多表联合查询
select * from 表1 as a join 表2 as b on a.id=b.lid where a.id =3;
select * from teacher as a join grade as b on a.id = b.tid where a.id = 1; as后面的a b是起的别名为了在where好写 这是查询a表id=1老师教的班级
select * from teacher as a join grade as b on a.id = b.tid where a.id = 2; as后面的a b是起的别名为了在where好写 这是查询a表id=2老师教的班级
select * from teacher as a left join grade as b on a.id = b.tid ; 加了left是意思是查左边表的所有内容左边表就是a表 如果改为right就是查询有表的所有信息
select * from grade where tid=(select id from teacher where name ='朱老师'); 子查询 先查出括号内表的信息 然后用这个信息来限制查询前面表的信息
select g.* from grade as g join teacher as t on g.tid=t.id where t.name='朱老师'; 这是用多表联合查询 这个查到的和上面这个子查询的结果是一样的,前面g.*号的意思是只查询g这个表的信息
8. union
操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
select sname as name,ssex as sex,sbirthday as birthday from student union select tname as name,tsex as sex,tbirthday as birthday from teacher;
9.in
select * from score where degree in(86,75);
select * from score where degree not in(86,75); not in就是排除这括号内值