查询数据(10种)
1:简单查询
select * from 表名称 ——查询表中所有数据 *代表所有列 select code,name from 表名称——查询指定列数据 select code,name from 表名称——查指定列的数据 select code as'代号',name as'姓名' from 表名称——给列指定别名
2:条件查询
select * from 表名 where code=' ' 查这一行 select * from 表名 where sex='true' and nation=' ' 表示并列,--多条件并的关系 select * from 表 名 where sex='true' or nation=' ' --多条件或的关系
3:范围查询
select * from 表名 where 列名>40 and 列名<50 select * from 表名 where 列名 between 40 and 50 --专用于范围查询
4:离散查询
select * from 表名 where 列名 in (' ',' ',' ') select * from 表名 where 列名 not in (' ',' ',' ') 反选,不在里面的
5:模糊查询
select * from 表名 where 列名 like '%宝马%'——查包含宝马的 select * from 表名 where 列名 like '宝马%'——查以宝马开头的 select * from 表名 where 列名 like '%宝马'——查以宝马结尾的 select * from 表名 where 列名 like '宝马'——查等于宝马的 select * from 表名 where 列名 like '--E'——查第三个是E的 % 代表是任意多个字符
- 下划线 代表是一个字符
6:排序查询
select * from 表名 order by 列名——默认升序排序 select * from 表名 order by 列名 desc——降序排列 select * from 表名 order by 列名 desc, 列名 asc——多个条件排序 , 前面是主条件 后面是次要条件 desc 降序 ,asc 升序, order by 排序 根据哪一列排序
7:分页查询
select top 5 * from 表名——查询前5条数据 select top 5 * from 表名 where code not in (select top 5 code from car)
8:去重查询(去掉重复的 针对某列)
select distinct 列名 from
9:分组查询
select Brand from 表名 group by Brand having count(*)>2
group by Brand 即表示根据一列分组 (根据品牌字段 'Brand'进行分组)
having后面跟筛选条件 count(*)>2 即每一组的数量大于2的数据
10:聚合函数(统计查询)
select count (*) from 表名——查询所有数据条数(每一列的) select count (列名主键) from 表名——查询这列的所有数据条数(执行快) select sum (列名) from 表名——求和 select avg (列名) from 表名——求平均值 select max (列名) from 表名——求最大值 select min (列名) from 表名——求最小值