查询语句


一、简单查询

1.最简单查询(查所有数据)
select * from 表名   注意:* 代表所有列,并不是代表所有行
例:select * from test

2.查询指定列
select 列名,列名 from 表名
例:select code,name from test

3.修改结果集的列名 as
select 列名 as '显示的字' from 表名
例:select code as '代号',name as '姓名' from test

4.条件查询
select * from 表名 where 条件
例:select * from test where code='n003'

5.多条件查询
或者 or:select * from 表名 where 条件 or 条件
例:select * from test where code='p003' or nation='n001'

并且 and:select * from 表名 where 条件 and 条件
例:select * from test where code='p004' and nation='n001'

6.范围查询 (某一列的内容是谁到谁之间的数据)
例:两种写法:查找汽车价格在40到60之间

(1)select * from car where price>=40 and price>=60
(2)select * from car where price between 40 and 60

7.离散查询
查询汽车价格在(10、20、30、40、50、60)中出现的信息 in
例:两种写法

(1)select * from car where price=10 or price=20 or price=30 or price=40 or price=50 or price=60

(2)select * from car where price in(10,20,30,40,50,60)

不在(10、20、30、40、50、60)中出现的信息 not in
例:select * from car where price not in(10,20,30,40,50,60)

8.模糊查询(关键字查询)like
%:任意n个字符
_:任意一个字符

查询汽车表名称中包含奥迪
例:select * from car where name like '%奥迪%'

查询汽车表名称第二个字符为“马”的汽车
例:select * from car where name like '_马%'

9.排序查询 order by
升序 asc,可省略
例:汽车表中价格列升序

select * from car order by price asc

降序 desc(从高到低)
例:汽车表中油耗列降序

select * from car order by oil desc

先a列升序后b列降序
例:汽车表中先将a列升序后将b列降序

select * from car order by a,b desc

10.去重查询 distinct
例:查找汽车表中型号一样的去重

select distinct brand from car

11.分页查询

一页显示m条数据 当前是第n页
limit (n-1)*m,m

一页显示10条数据 当前是第二页 跳过多少条,取多少条
例:select * from chinastates limit 10,10


12.聚合函数(统计函数)
(1)总数 count(*):查询数据总条数
例:select count(*) from chinastates

count(主键列 areacode)
例:select count(areacode) from chinastates

(2)求和 sum(求价格和列)
例:select sum(price) from car

(3)求平均 avg(求价格平均列)
例:select avg(price) from car

(4)取最大值、最小值(价格列)
例:

select max(price) from car
select min(price) from car

13.分组查询 group by
查询汽车表中每个系列下有多少汽车
例:select brand,count(*) from car group by brand

查询汽车表中卖的汽车数量大于3的系列    注意:  group by....having(条件)
例:select brand from car group by brand having count(*)>3

二、高级查询


1.连接查询,对结果集列的扩展
select * from info,nation      #形成很大的冗余(笛卡尔积)
多张表的列有重名的,要写表名,然后写列名,格式如下:表名.列名
两种方式:
(1)select * from info,nation where info.nation=nation.code

select info.code,info.name,sex,nation.name,birthday from info,nation where
info.nation=nation.code

(2)select * from info join nation on info.nation=nation.code

2.联合查询,对结果集行的扩展, 列的数量要相同 union
select code,name from info
union
select code,name from nation

3.子查询
父查询:外层查询
子查询:里查询(查询结果作为父查询的条件)

(1)无关子查询:子查询在执行时和父查询没有关系(子查询可单独执行)
a.查询民族为汉族的所有人员信息
父查询:select * from info where nation=()
子查询:select code from nation where name='汉族'
合并后就是结果:
select * from info where nation=(select code from nation where name='汉族')

b.查询系列名是“宝马5系”的所有汽车信息
select * from car where brand=(select brand_code from brand where brand_name='宝马5系')

(2)相关子查询:子查询在执行时和父查询有关系(子查询不可单独执行)
a.查询汽车表中油耗小于该系列平均油耗的所有汽车信息
父查询:select * from car where oil<(该系列平均油耗)
子查询:select avg(oil) from car where brand=该系列
合并后就是结果:
select * from car as a where oil<(select avg(oil) from car as b where b.brand=a.brand)
注意:用as修改表名时不用加引号''

原文地址:https://www.cnblogs.com/zhaohui123/p/7418037.html