数据库-T-SQL 语句-简单查询
2016-03-28 16:14
211 查看
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 #跳过几条数据取几条
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 #跳过几条数据取几条
相关文章推荐
- Sql--运算符优先级
- MySQL系列:事务机制
- 关于Mongodb的一些小问题
- MySQL远程访问权限,允许远程连接
- 数据库设计原则
- MyBatis的动态SQL详解
- mysql之limit m,n
- Oracle表的并行度
- sqlite的callback
- 统计mysql下的binlog里的语句执行次数
- MongoDB - 内嵌角色
- oracle常用sql
- MYSQL中replace into的用法
- postgresql 查看数据库,表,索引,表空间以及大小
- Oracle收集表的数据与统计信息差异
- SQL
- mysql 随机查询 记录集
- Oracle行转列和列转行
- Hibernate之mysql-connecter jdbc error 一
- Java redis 之 Jedis