数据查询语句
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)
4442

被折叠的 条评论
为什么被折叠?



