数据库-T-SQL 语句-简单查询

1.普通查询

select * from Info #查询所有内容

select Code,Name from Info #查询指定列

2.条件查询

select * from Info where Code ='p001'  #筛选行,单条件

select * from Info where Nation = 'n001' and Sex = true  #多条件查询 并的关系

select * from Info where Sex = false or Nation = 'n002'  #多条件查询 或的关系

3.模糊查询

select * from ChinaStates where AreaName like '中%'  #  %代表任意多个字符

select * from ChinaStates where AreaName like '%城%'

select * from ChinaStates where AreaName like '_ 城%' # _表示任意一个字符,“城”在第二个位置出现

4.排序查询

默认以主键值升序排列

asc 升序

select * from Car order by Code desc    #order by 列名 desc    根据Code列降序排列

select * from Car order by Brand    #根据Brand列默认升序排列

select * from Car order by Brand,Powers  #根据两个条件排序 以第一个为主 第一个一样,再按第二个排序

5.统计查询(聚合函数)

select count(*) from Car   #查询总条数

select max(Price) from Car  #查询某一列的最大值

select min(Price) from Car  #查询某一列的最小值

select avg(Price) from Car #查询某一列的平均值

select sum(Price) from Car  #查询某一列的总和

6.分组查询

select Brand,count(*) from Car group by Brand  #以Brand 分组 并查询每组里有多少条数据

select * from Car group by Brand having count(*)>2 #以Brand 分组 并查询了每组里数据大于2的  用having 筛选

7.分页查询

(页数-1)*每页条数

select * from Car limit 0,5  #跳过几条数据取几条

原文地址:https://www.cnblogs.com/yifangtongxing/p/5326131.html