sql 基础语句
来源:互联网 发布:嘉实优化红利基金咋样? 编辑:程序博客网 时间:2024/06/06 00:48
Select top 3 * from class order by id desc 表示查询前3条的数据并且按降来排
select distinct 年龄 from class 去除重复行的查询
select top 2 * from class order by newid() 表示随便查询2行数据
select * from class where 年龄=20 or 年龄=19 and 性别='男' 表示先查询年龄等于19 并且是男的 在查询所有年龄等于20的人 先执行AND查询 再执行OR
select * from class where id not in (select id from class1) 表示查询表一中在表2中没有的数据
select * from class where id between 1 and 5 表示查询1到5的数据
select * from class where id not between 1 and 5 表示查询不是1到5的数据
select * from class where 姓名like '刘%' 表示查询刘开头的人
select * from class where 姓名like '%丹%'查询包含丹的人
select * from class where 姓名like '[刘丹]%'查询以刘或者以丹开头的数据
select * from class where 姓名like '[%刘丹]%'查询包含丹或者刘的数据
select * from class where 年龄like '[^1-2]%'查询数据不是以1到2之间开头的
select * from class where 年龄like '[^刘陈]%' 查询不是以刘或者陈开头的数据
select * from class where 工作地址is null 表示查询工作地址不是空值的数据
select * from class where 工作地址is not null 查询不是空植的数据
select * from class where id <> all(select id from class1)表示查询表2在表一中没有的ID数据
select * from class where id = any(select id from class1) 查询表一中和表2相同的ID
elect * from class where (性别not in ('男'))and (not (年龄between 18 and 21))
not语句的查询 在条件语句前面加上NOT
select top 3 * from class order by id 前面最三个数据
use liudan
select top 3 * from class order by id desc 最后三个的数据
select ltrim(rtrim(姓名)) from class 里面函数RTRIM代表删除结尾空格数据 外面函数 ltrim 删除前面空格的数据
select 姓名+',' as 姓名,lower(gege) as gege from class 给姓名的每条数据加上 ,; 把gege 列转换为小写
select 姓名+',' as 姓名,upper(gege) as gege from class 后面函数把数据全部转换为大写
select * from class where month(日期)=9 and year(日期)=1993 and day(日期)=9
分别是三个函数 第一个是对指定的日期 月 ,年,日
select 年龄-id as a from class 表示用每一行的数据用年龄-id得到的数据+ - * / 都可以用
select sum(年龄) as a from class 求和
select * from class where 年龄>(select avg(年龄) from class) 求年龄大于平均值的数据
select * from class where 年龄= (select max(年龄) from class) 求年龄最大的、
select * from class where 年龄= (select min(年龄) from class) 年龄最小的人
select count(*) from class 表中数据总条数
select count(年龄) from class 查询年龄不为空的
select sum(年龄) from class where 性别='男' 得到性别为男的的总年龄数
select 年龄,count(*) from class group by 年龄 表示对年龄进行汇总,就是说 对相同年龄的人人数进行汇总
select 年龄,count(*) from class group by 年龄having 年龄>20 对年龄大于20的才进行汇总
select 年龄,count(*) from class group by 年龄having (年龄 in (20)) 年龄在20范围内的汇总
select * from class union select * from class3 将两个表连接到一起来 删除重复的行
select * from class union all select * from class3 保留重复的行
select id from class intersect
select id from class1 两个表相同的数据 这里因为没有两个相同的表所以只查询ID相同的
select * from class except select * from class3 两个相同的表的不相同的数据
insert into class3 (姓名)values ('dadad') 表示在表中的姓名列插入一个数据 只所以要写是哪个列的数据 是因为不用我插入id了 我门一般设置id是自动生成的 所以再这里要注明;;
update class3 set 工作地址='湖北' where id=2 对ID=2的行 的工作地址的列进行修改
update class3 set 工作地址='湖北' 对所有工作地址的列进行修改
delete top(1) class 删除表中前1条的数据
delete from class3 where id=2删除ID=2的行
select top 3 * from class order by newid() 随机查询3条数据
select * from class order by 名字 collate chinese_prc_cs_as 按音序查询
select distinct 年龄 from class 去除重复行的查询
select top 2 * from class order by newid() 表示随便查询2行数据
select * from class where 年龄=20 or 年龄=19 and 性别='男' 表示先查询年龄等于19 并且是男的 在查询所有年龄等于20的人 先执行AND查询 再执行OR
select * from class where id not in (select id from class1) 表示查询表一中在表2中没有的数据
select * from class where id between 1 and 5 表示查询1到5的数据
select * from class where id not between 1 and 5 表示查询不是1到5的数据
select * from class where 姓名like '刘%' 表示查询刘开头的人
select * from class where 姓名like '%丹%'查询包含丹的人
select * from class where 姓名like '[刘丹]%'查询以刘或者以丹开头的数据
select * from class where 姓名like '[%刘丹]%'查询包含丹或者刘的数据
select * from class where 年龄like '[^1-2]%'查询数据不是以1到2之间开头的
select * from class where 年龄like '[^刘陈]%' 查询不是以刘或者陈开头的数据
select * from class where 工作地址is null 表示查询工作地址不是空值的数据
select * from class where 工作地址is not null 查询不是空植的数据
select * from class where id <> all(select id from class1)表示查询表2在表一中没有的ID数据
select * from class where id = any(select id from class1) 查询表一中和表2相同的ID
elect * from class where (性别not in ('男'))and (not (年龄between 18 and 21))
not语句的查询 在条件语句前面加上NOT
select top 3 * from class order by id 前面最三个数据
use liudan
select top 3 * from class order by id desc 最后三个的数据
select ltrim(rtrim(姓名)) from class 里面函数RTRIM代表删除结尾空格数据 外面函数 ltrim 删除前面空格的数据
select 姓名+',' as 姓名,lower(gege) as gege from class 给姓名的每条数据加上 ,; 把gege 列转换为小写
select 姓名+',' as 姓名,upper(gege) as gege from class 后面函数把数据全部转换为大写
select * from class where month(日期)=9 and year(日期)=1993 and day(日期)=9
分别是三个函数 第一个是对指定的日期 月 ,年,日
select 年龄-id as a from class 表示用每一行的数据用年龄-id得到的数据+ - * / 都可以用
select sum(年龄) as a from class 求和
select * from class where 年龄>(select avg(年龄) from class) 求年龄大于平均值的数据
select * from class where 年龄= (select max(年龄) from class) 求年龄最大的、
select * from class where 年龄= (select min(年龄) from class) 年龄最小的人
select count(*) from class 表中数据总条数
select count(年龄) from class 查询年龄不为空的
select sum(年龄) from class where 性别='男' 得到性别为男的的总年龄数
select 年龄,count(*) from class group by 年龄 表示对年龄进行汇总,就是说 对相同年龄的人人数进行汇总
select 年龄,count(*) from class group by 年龄having 年龄>20 对年龄大于20的才进行汇总
select 年龄,count(*) from class group by 年龄having (年龄 in (20)) 年龄在20范围内的汇总
select * from class union select * from class3 将两个表连接到一起来 删除重复的行
select * from class union all select * from class3 保留重复的行
select id from class intersect
select id from class1 两个表相同的数据 这里因为没有两个相同的表所以只查询ID相同的
select * from class except select * from class3 两个相同的表的不相同的数据
insert into class3 (姓名)values ('dadad') 表示在表中的姓名列插入一个数据 只所以要写是哪个列的数据 是因为不用我插入id了 我门一般设置id是自动生成的 所以再这里要注明;;
update class3 set 工作地址='湖北' where id=2 对ID=2的行 的工作地址的列进行修改
update class3 set 工作地址='湖北' 对所有工作地址的列进行修改
delete top(1) class 删除表中前1条的数据
delete from class3 where id=2删除ID=2的行
select top 3 * from class order by newid() 随机查询3条数据
select * from class order by 名字 collate chinese_prc_cs_as 按音序查询
- SQL查询语句基础
- 基础Sql语句
- 最基础sql语句
- SQL语句使用基础
- Sql基础语句
- Sql 基础语句
- 2-基础SQL语句
- SQL语句收集--基础
- 基础sql语句
- Sql语句最基础
- SQL基础语句+说明
- sql管理基础语句
- Sql基础语句【整理】
- sql语句基础
- sql语句基础
- 基础类 - SQL语句
- SQL基础实用语句
- SQL语句基础
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型
- get_template_part() 函数
- MyDAC in thread
- oracle 数据恢复
- Access 2007使用技巧小结
- sql 基础语句
- 黑马程序员_java环境变量的设置
- (转)Notification使用详解之二:可更新进度的通知
- 19个A/B测试的贴士,总有一些对你有用
- get_template_part()详解
- cxf 错误总结
- 黑马程序员_基础知识
- JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
- Oracle中创建DBLink