mysql--DQL语句

数据查询语句

1.查询全部
select * from t

2.指定列
select a,b,c… from t

3.给列起别名 便于观看,方便二次查询 as
select a as A from t

4.给表起别名便于观看,方便二次查询,又时把子查询作为表时,必须起别名 as
select * from t as t1

5.运算符:可以对列进行一定的算术运算(+ - * /)或者函数调用(fountion()),可以使用在一切普通列可以使用的位置
select a+b as total from t

6.条件运算符where ,=> = < <> = > < !=
select * from t where t.a <=条件

7.between and
相当于 [前条件,后条件],<=后条件 and >=前条件
,between 前条件 and 后条件

8.like 模糊查询_ , %
select * from t where name like ‘__’
select * from t where name like ‘李%’
select * from t where name like ‘李_’

9.排序,order by a asc ,b desc 如果不指定,则默认插入顺序
select * from t order by id

10.限制行查询 limit a,b 从a到b行 limit a 从0到a行
select * from t limit a
select * from t limit a,b

11.分组和聚合函数 group by a,b,c…
avg() sum() max() min() 聚合函数必须在分组查询中,如果未使用group by ,则默认整表为一个分组。聚合函数就是对一组数据进行统计。
select count(*) from t group by sex
select max(age) from t_age

12.子查询:用在条件中
select a from t where t.id = (select 语句)
select (select语句)from t where 条件
select 列 from(select语句)

12.1比较运算符< > = != 等接子查询,子查询中只能有一个数据

12.2 in 和 not in 接子查询,子查询中可以有多个数据,但只能有一列
select * from t where id in(1,2,3)
select * from t where id in (select id from t where t.sex = ‘女’)

12.3如果需要有多列,则使用all(数据) 或者 any (数据)
select * from t where any(数据)
select * from t where all(数据)

13.having表示在查询之后二次查询(二次筛选)
select count(0) from t_employee group by t_employee.department_id having count(0) >3

14.子查询,还可以出现在列中,还可以出现在from后面,还可以作为where的第一个操作参数
相关子查询,查询过程中,使用到外部查询的相关列,但是性能极低
select (select)from

select a from (select)

select a from t where b =(select)

select a from t where (select) = b

15.表连接
内连接:select * from t_a inner join t_b on 表连接条件

外连接
左连接:对于表连接的列,如果条件不满足,右表允许为null,左表全部显示,LEFT JOIN或LEFT OUTER JOIN
select * from t_a left join t_b on 表连接条件

右连接:对于表条件的列,如果条件不满足,左表允许为null,RIGHT JOIN 或 RIGHT OUTER JOIN
select * from t_a right join t_b on 表连接条件

全外连接:mysql不支持,但是sqlsever等支持,FULL JOIN 或 FULL OUTER JOIN
select * from t_a full join t_b on 表连接条件

16.union:联合查询,把两个结果集联合到一块
两个结果集的列数,和数据类型以及语义要尽量保持一质
(select 语句1 )union (select 语句2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A little sea pig

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值