常用经典SQL语句
来源:互联网 发布:gencode数据库 编辑:程序博客网 时间:2024/06/05 12:40
本文将列出一些在平常开发过程中会用到的比较经典的SQL语句,常常用于实现一些特别的功能.
1.SQL插入语句得到自动生成的递增ID值
Insert into Table1(Name,des,num) values ('ltp','thisisbest',10);
select @@identity as 'Id'
2.实现是1或0想显示为男或女
select name,sex=
case Sex
when '1' then '男'
when '0' then '女'
end
from Tablename
3.嵌套查询
select a,b,c from Table1 where a IN(select a from Table2)
4.显示文章、提交人和最后回复时间
select a.title,a.username,b.adddate
from tablename a,(select max(adddate) adddate from tablename where tablename.title=a.title) b
5.在同一表内找相同属性的记录
select UserID from Accounts_Users
where UserName is not null
group by UserID
having count (*) >1
6. 查询类别所有的产品对应数据
select CategoryName,ProductName
from Categories left join on Products
on Categories.CategoryID=Products.CategoryID
7.按范围查询编号在2到5之间的用户信息
select * from UserValue where UserID between 2 and 5
8.日程安排提前5分钟提醒
select * from TabSchedule where datediff(minute,getdate(),开始时间)<5
9.按姓氏笔画排序
select * from TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
10.得出某日期所在月份的最大天数
select day(dateadd(dd,-day('2008-02-13'),dateadd(mm,1,'2008-02-13') ) )
as 'DayNumber'
11.通配符的一些用法
(1)select * from tablename where column like '[A-M]%'
这样可以选择出column 字段中首字母在A-M之间的记录
(2)select * from tablename where column like '[ABC]%' .
这样可以选择出column 字段中首字母是A或者B或者C的记录
(3)select * from tablename where column like '[A-CG]%'
这样可以选择出column 字段中首字母在A-C之间的或者是G的记录
(4)select * from tablename where column like '[^C]%'
这样可以选择出column 字段中首字母不是C的记录
单字符(关键字:like_):
通过使用下划线字符(_),可以匹配任意单个字符
select * from tablename where column like 'M_crosoft'
匹配特殊字符:([ ]_-%)
select * from tablename where column like '%[%]%'
13.复制表结构(只复制结构,源表名: 目标表名:)
select * from into b from a where 1<>1
或
select top 0 * into [b] from [a]
14.复制表数据(复制数据,源表名: 目标表名:)
Insert into b(Name,des,num) select Name,des,num from Table1
15.编辑一个列
增加列:
alter table Table1 add username varchar(30) not null default ' '
修改列:
alter table Table1 alter column username varchar(40)
删除列:
alter table Table1 drop column username
16.修改该时间字段的小时部分
--把所有时间2006-11-1的数据23点修改为21点
update ad_Browse_20061101
set browsetime='2006-11-01 21'+
substring(convert(varchar(30),browsetime,8),3,7)
where datepart(hour,browsetime)=23
- 常用经典SQL语句
- 常用经典SQL语句
- 常用经典SQL语句
- 经典常用SQL语句
- 常用经典SQL语句
- 常用经典SQL语句
- 常用经典SQL语句集锦
- 常用经典SQL语句大全
- 常用经典SQL语句(收藏)
- 常用经典SQL语句大全
- 我常用经典SQL语句[就先写一句吧]
- 经典常用精妙的SQL语句
- oracle中常用经典sql查询语句。
- 常用经典SQL语句集合(收集)
- 【整理】常用经典SQL语句大全
- 50个经典常用的sql语句
- 常用经典SQL语句(收藏) 实用SQL语句
- 经典SQL语句大全、50个常用的sql语句
- poj 2676 Sudoku 破水题废了我一天时间 其实一点含量都没有 郁闷。。。想多了。。。
- 独立电子商务网站运营成本
- JavaMail API细节详细解析大全
- OAF中给sequence类型的id赋值
- html:text的属性控制
- 常用经典SQL语句
- hdu1398
- CI数据库操作整理
- 看视频全屏出现白屏问题,解决办法
- OBJECTPROPERTY用法整理
- windows编程笔记1
- c和c++的一点区别
- SQL语句得到存储过程关联哪些表名
- 读书笔记一则