数据库---T-SQL语句:查询语句(二)
2016-01-17 20:12
555 查看
[b]>查询:[/b]
[b]一.查询所有数据:[/b]
select * from Info ---查询所有数据(行)
select Name from Info ---查询特定列(Name列)
select Name,Code from Info ---查询特定两列(Name和Code列)
[b]二.根据条件查[/b]
select * from Info where Code='p001' 一个条件查询(遍历每一个数据查出来的)
select * from Info where Code='p001' and Nation='n003' 多条件 并关系 查询
select * from Info where Name='胡军' or Nation='n001' 多条件 或关系 查询
select * from Car where Price >=50 and <=60 范围查询(可以用,不建议)
select * from Car where Price between 50 and 60 范围查询(推荐)
[b]三. 模糊查询(也属于条件查询,模糊查询是针对字符串查询)[/b]
select * from Car where Name like '%奥迪' ----%是通配符,代表任意N个字符
select * from Car where Name like '%奥迪%' ---代表着在Name中只要有奥迪这个字符串就可以 前后都可以有N个字符串
select * from Car where Name like '_奥迪' _通配符:代表任意一个字符
[b]四.排序[/b]
select * from Car order by Price (asc) 按照价格升序排列(默认的是升序排列)
select * from Car order by Price desc 按照价格降序排列
select * from Car order by Price desc ,Oil desc ( 谁写在前面先排谁) 按照两列进行排序,前面的为主要的
[b]五. 统计函数(聚合函数)[/b]
select count(Code) from Car 查询表中有多少条数据
//如果括号里的是*,那么就遍历每一条每一列数据;如果括号里是Code,那么就只看Code里的数据,如果有数据,就算一个;为了执行更快一些,一般用主键
select max(Price) from Car 取价格的最大值
select min(Price) from Car 取价格的最小值
select sum(Price) from Car 取价格的总和
select avg(Price) from Car 取价格的平均值
[b]六.分组查询[/b]
select * from Car group by Brand → select Brand from Car group by Brand → select count(Brand) from Car group by Brand
select Brand from Car group by Brand having conut(*)>2 查询所有系列中数量大于2的
[b]七.分页查询[/b]
select * from Car limit 5,5 跳过几条(前面的参数)数据取几条数据 (后面的参数)
select top5 from Car (在sql server中表示取前五条数据,但是在MySQL中不能使用)
[b]八.去重查询[/b]
select distinct from Brand from Car (只保留第一个)
[b]一.查询所有数据:[/b]
select * from Info ---查询所有数据(行)
select Name from Info ---查询特定列(Name列)
select Name,Code from Info ---查询特定两列(Name和Code列)
[b]二.根据条件查[/b]
select * from Info where Code='p001' 一个条件查询(遍历每一个数据查出来的)
select * from Info where Code='p001' and Nation='n003' 多条件 并关系 查询
select * from Info where Name='胡军' or Nation='n001' 多条件 或关系 查询
select * from Car where Price >=50 and <=60 范围查询(可以用,不建议)
select * from Car where Price between 50 and 60 范围查询(推荐)
[b]三. 模糊查询(也属于条件查询,模糊查询是针对字符串查询)[/b]
select * from Car where Name like '%奥迪' ----%是通配符,代表任意N个字符
select * from Car where Name like '%奥迪%' ---代表着在Name中只要有奥迪这个字符串就可以 前后都可以有N个字符串
select * from Car where Name like '_奥迪' _通配符:代表任意一个字符
[b]四.排序[/b]
select * from Car order by Price (asc) 按照价格升序排列(默认的是升序排列)
select * from Car order by Price desc 按照价格降序排列
select * from Car order by Price desc ,Oil desc ( 谁写在前面先排谁) 按照两列进行排序,前面的为主要的
[b]五. 统计函数(聚合函数)[/b]
select count(Code) from Car 查询表中有多少条数据
//如果括号里的是*,那么就遍历每一条每一列数据;如果括号里是Code,那么就只看Code里的数据,如果有数据,就算一个;为了执行更快一些,一般用主键
select max(Price) from Car 取价格的最大值
select min(Price) from Car 取价格的最小值
select sum(Price) from Car 取价格的总和
select avg(Price) from Car 取价格的平均值
[b]六.分组查询[/b]
select * from Car group by Brand → select Brand from Car group by Brand → select count(Brand) from Car group by Brand
select Brand from Car group by Brand having conut(*)>2 查询所有系列中数量大于2的
[b]七.分页查询[/b]
select * from Car limit 5,5 跳过几条(前面的参数)数据取几条数据 (后面的参数)
select top5 from Car (在sql server中表示取前五条数据,但是在MySQL中不能使用)
[b]八.去重查询[/b]
select distinct from Brand from Car (只保留第一个)
相关文章推荐
- 机器学习-非监督学习(Unüberwachte Lernverfahren)+大型数据库:Clustering-2
- 18-mysql优化之索引的增删改以及优化
- MySQL: Change root password in Mac
- Redis安装部署学习
- Redis系列三:Redis常用设置
- Oracle 修改用户名
- SQliteDatabase 中sql语句引用字符串时的注意点,要把单引号放进去,E/SQLiteLog﹕ (1) no such column:
- SQliteDatabase 中sql语句引用字符串时的注意点,要把单引号放进去,E/SQLiteLog﹕ (1) no such column:
- ※ 关于MySQL的1067错误解决方法 ※
- SqlServer中GO关键字的理解
- 浅析Mysql的my.ini文件
- 使用yum快速部署Oracle安装环境(11g)
- MySql计算两个日期时间的差函数
- sql中修改sa密码, 查看、设置端口号
- mongodb启动失败解决方案
- 新PaaS平台上线数据初始化经验
- Oracle释放高水位线
- centos7中mysql(mariadb)安装和启动
- oracle 连接之后出现 no logged on 问题
- 关于oracle用户名登录