<span style="font-size:18px;"># 一单表查询 :
# 1.查询所有字段 select *from 表名
select *from student
# 2.查询指定字段(所有字段和指定字段) select 列名 from 表名 select t.id,t.name from t
select s.studentname,s.studentsex from student s
# 3 查询指定记录 select 字段名1,字段名2 from 表名 where 查询条件(>,<,=,between,>=,>=)
select s.studentname,s.studentsex from student s where s.studentsex='男'
# 4 带in关键字查询(指定范围内记录)
select *from orders
select *from orders o where o.price IN(100,200)
# 5.order by (根据什么排序) asc(升序) desc(降序) 默认为升序
select *from orders o where o.price IN(100,200) order by o.price desc
# 6.带between 范围 and 范围查询
select *from orders o where o.price between 100 and 150
# 7.带like的字符匹配查询 搭配 % 和 _ 进行条件查询
# 结合%统配符
select *from orders o where o.order_number like "%1"; #以1结尾
select *from orders o where o.order_number like "%0%"; #只要中间存在0就查询
select *from orders o where o.order_number like "0%1"; #以0开头,以1结尾
# 结合下划线统配符'_ ,只能匹配一个单个字符,如果要匹配多个需要使用多个"___"
select *from orders o where o.order_number like "___1";
# 8.查询空值 is NULL :设计者可以指定某列中是否存在空字段(NULL),空值不同于0,也不同于空字符串
select *from customer
select *from customer c where c.city is null;
# 9 带and的多条件查询 : 需要同时满足
select *from student s where s.classid> 2 and s.studentsex ='男'
# 10 带or的多条件查询 : 满足一个即可
select *from student s where s.classid > 2 or s.classid = '男'
# 11 查询不重复的结果 select distinct 字段名 where 表名
select distinct s.studentage from student s order by s.studentage
# 使用order by 进行排序,可以是多列,也可以是单列
# 12 分组查询 group by 关键字通常与聚合函数一起使用 max() min() avg() count() sum()
# 查询每个年龄段的学生人数
select s.studentage 年龄, count(*) 总人数 from student s group by s.studentage
# 显示每个分组的字段 显示在这个阶段的学生名字 grounp_concat()
select s.studentage 年龄, group_concat(s.studentname) 姓名 from student s group by s.studentage
# 使用having进行过滤分组 限定显示记录满足的条件
select s.studentage 年龄, group_concat(s.studentname) 姓名
from student s group by s.studentage having s.studentage > 20
# 查询满足条件后增加一列总数 with rollup
select s.studentage 年龄, group_concat(s.studentname) 姓名
from student s group by s.studentage with rollup
# 13 一般分页使用较多 使用limit限制查询结果数量 limt[位置偏移量] 行数
select *from student limit 4; # 只查出前4个
# 从第3条记录开始,查询2条jil
select *from student limit 3,2;
# 常用聚合函数有 : avg() 求平均数 count() 求总数 max()求最大值 min()求最小值 sum()求总和
# 二.多表 连接查询【关系型数据库最主要的查询】 : 内连接,外连接,
# 1.交叉连接(了解)
select *from customer cross join orders
# 2.内连接 (inner join): 进行表与表之间某些列数据比较操作,查询并组成新的记录,只有满足记录的才能出现
# 隐式内连接 不使用on关键字(即不明确指定连接条件),使用的是where
# 查询有订单的客户的信息和订单信息
select *from customer c , orders o where c.id = o.customer_id;
select c.name '姓名' from customer c , orders o where c.id = o.customer_id;
# 显示内连接 使用on关键字
# 查询有订单的客户的信息和订单信息
select *from customer c inner join orders o on c.id = o.customer_id;
# 查询订单金额200以上
select *from customer c inner join
orders o on c.id = o.customer_id where price > 200
# 3. 外连接 左外 left join 右外 right join
# a.左外连接:返回满足连接条件的结果,同时返回左表中剩余的其他记录
# 查询客户信息,同时显示他的订单
select *from customer c left outer join orders o on c.id = o.customer_id
# 查询所有的员工,打印所在的部门名称
SELECT * FROM employee e LEFT OUTER JOIN department d ON e.depart_id=d.id;
# b.右外(与左外相反)返回满足连接条件的结果,同时返回右表中剩余的其他记录
# 查询所有订单,同时显示他的客户信息
select *from customer c right outer join orders o on c.id = o.customer_id
# 三 子查询:嵌套查询(内部语句)。一个查询语句是另外一个查询语句的条件。子查询的语句必须放在小括号之内
# select * from t1 where id=(select id from t2);
# 查询id为2的老师教过的学生姓名
# 方式二:连接查询
select s.* from teacher_student ts,student s where ts.s_id=s.id and ts.t_id=2;
#方式三:子查询(一个单列结果)
select * from student where id in (select s_id from teacher_student where t_id=2);
# 查询名字为'陈冠希'的所有订单信息(子查询)
select *from orders where customer_id = (select id from customer where name='陈冠希');
# 四 合并查询 union[all] 不使用all表示执行时候删除重复记录,所有返回行都唯一,使用all是查询所有并自动排序
# 使用联合查询:查询客户id=1或者订单金额>=200的订单信息]
select *from orders
SELECT * from orders where price>=200 or customer_id=1;
select *from orders where price >= 200 union select *from orders where customer_id = 1;
</span>
MySql查询语句总结
最新推荐文章于 2023-08-08 21:59:23 发布