数据库的简单查询

1、最简单查询(查所有数据)
select * from 表名; 注:* 代表所有列
select * from info

2、查询指定列
select code,name from info

3、修改结果集的列名
select code as '代号',name as '姓名' from info

4、条件查询
select*from info where code='p003'


5、多条件查询

①查询info表中code为p003或者nation为n001的所有数据
select * from info where code='p003' or nation='n001'

or:或的关系。


查询info表中code为p004并且nation为n001的数据
②select * from info where code='p004' and nation='n001'

and:和的关系。

6、范围查询

between:在某某之间。


复杂写法:select*from car where price>=40 and price<=60

简单写法:select*from car where price between 40 and 60

7、离散查询
查询汽车价格在(10、20、30、40、50、60)中出现的汽车信息。

in():在括弧里出现的。

not in():不在括弧里出现的。

复杂写法:select * from car where praice=10 praice=20 praice=30 praice=40 praice=50 praice=60

简单写法:select * from car where price in(10,20,30,40,50,60)


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

8、模糊查询(关键字查询)like

like:名称前面加。
%:任意多个字符。
_下划线 任意一个字符。

①查询汽车表里面名称包含奥迪的。
select * from car where name like '%奥迪%'


②查询汽车表中名称第二个字符为‘马’的汽车
select * from car where name like '_马%'

9、排序查询
asc:升序(可省略)
desc: 降序
order by:  排序
①升序排序:select * from car order by price asc

 
先按照brand升序排,再按照price降序排。
②降序排序:select * from car order by brand,price desc

10、去重查询
select distinct brand from car

11、分页查询

一页显示十条,当前是第二页。
linmit:分页
select * from chainastates limit 10(跳过的数据),10(取多少条)

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

12、聚合函数(统计函数)

select count(areacode) from chinastates  查询数据总条数


select sum(price) from car   求和
select avg(price) from car   求平均
select max(price) from car  求最大值
select min(price) from car   求最小值

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

②查询汽车表中卖的汽车数量大于3的系列
select brand from car group by brand having count(*)>3

 

原文地址:https://www.cnblogs.com/zrifq/p/6125913.html