MySQL表查询操作命令

1.基本查询语句
语法:
select 属性列表
from 表名(视图)列表
[where 条件表达式 1]
[group by 属性名 1 [having 条件表达式 2]]
[order by 属性名 2 [asc | desc]]
asc:升序
desc:降序
2.字段查询
例子:
select * from subject; /*查询表中所有数据*/
3.查询指定字段
select SubjectName from subject;
4.查询指定记录
select SubjectName from subject where SubjectNo = 1 ;
5.查询条件 符号或关键字
比较 =、<、<=、>、>=、!=、<>、!>、!<
指定范围 between and、not between an
指定集合 in、not in
匹配字符 like、not like
是否为空 is null、is not null
多个查询条件 and、or
6.查询结果去重 distinct
select distinct SubjectNo from subject;
7.order by(默认是升序排列)[asc | desc]
select SubjectName from subject order by SubjectNo;
--多字段排序:
先按照第一个字段排序,如果遇到相同数据再按照第二个字段排序
8.group by    分组查询
8.1 单独使用group by ,会显示每个组的一条记录
8.2 group_concat() 结合使用-----------以sex分组,获得每组的name
select sex ,group_concat(name) from employee group by sex;
8.3 与聚合函数结合使用------------以sex分组,获取每组成员的个数
select sex ,count(sex) from employee group by sex;
8.4 与where结合使用,限制查询条件
8.5 与having结合使用,筛选查询的结果


8.6 多个字段分组
select * from employee group by id , name ;
8.7 与with rollup结合使用-----会加上一条数据,该数据显示所有查询到的数据的总和
select * from employee group by id , name with rollup;
8.8 限制查询结果数量
select * from employee group by id limit 3; /*显示三条查询到的结果*/
select * from employee group by id limit 0,10; /*显示下标从0到10的查询结果*/
9.聚合函数查询 
count
select count(id) from employee ; /*查询字段id中所有记录的数量*/
sum(),avg(),max(),min()  同count的用法
10.链接查询
10.1 内连接查询(where | inner join...on... )
(内连接是完全匹配,即 A 表与 B 表一一对应,一方没有,则该记录不会被查出)
10.2 左外链接 (left join...on)
(以左表为主表,显示主表的所有信息,而与右表无匹配信息时则显示 NULL)
10.3 右外连接 (right join...on)
(以右表为主表,显示右表中的所有信息)
10.4 复合查询 (inner | left | right join...on...where...)
11. 子查询
子查询:将一个查询语句嵌套在另一个查询语句中。内层查询语句的查询结果可为外层查询提供条件
11.1 带in关键字 [not in]
select * from employe where id in (select id from department);
11.2 带比较运算符
11.3 带exists关键字
11.4 带any关键字(满足子查询中的其一即可,则执行父查询)
select SubjectName,GradeId from subject
where GradeId >= any
(select GradeId from grade);
11.5 带all关键字(满足子查询中的所有,则执行父查询)
select SubjectName,GradeId from subject
where GradeId >= all
(select GradeId from grade);
12. 合并查询结果
12.1 union (合并查询结果去处重复)
select * from employee
union
select * from department;
12.2 union all (合并查询结果不去除重复)
13. 为表和字段取别名  as
13.1 为表取别名
select * from employee as 员工 where id < 11 ;
13.2 为字段取别名
select studentName as 学生姓名 from student;
as子句作用
可以给数据列取一个新别名
可以给表取一个新别名
可把经计算总结的结果用另一个新的名称来代替
注意:as也可以省略不写
13.3 select concat(email,@ibeifeng.com) as email from student; --查询邮箱
14.like 关键字(模糊查询)
select name from class where name like '刘%'['刘_'];
%表示匹配所有
_代表匹配一个字符
15.in 关键字 子查询(在一个范围内查找)
select name from class where age in (10,11);
16.注意点:
--数值数据类型的记录之间才能进行算数运算;
--相同数据类型的数据才能进行比较;
17. limit 限制查询
select * from tableName limit 5; #返回查询结果的前五条数据
select * from tableName limit 5,10; #返回查询结果的第6-10条数据
18.union 合并查询 
union 合并完全相同数据 (相同的数据显示一条,和不相同的数据一起显示)
union all  显示两张表查询出来的数据,不管相同不相同都会显示
eg:
select name from subject
union [all]
select name from myclass ;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值