您的位置:首页 > 数据库

常用经典的SQL语句

2012-05-30 20:28 381 查看
1、写出一条sql语句:取出表a中第31到第40记录(sqlserver,以自动增长的id作为主键,注意:id可能不是连续的。

  演变过程:1.select top 30 S_ID from StuInfo --先取出前30条数据

  2.select *from StuInfo where S_ID not in (select top 30 S_ID from StuInfo) --取出S_ID不等于前30的

  3.select top 10* from StuInfo where S_ID not in (select top 30 S_ID from StuInfo)--S_ID不再前30条的前10条数据

  方法1:select top 10* from StuInfo where S_ID not in (select top 30 S_ID from StuInfo)

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、编辑一个列

增加列:

alter table Table1 add username varchar(30) not null default ''

修改列:

alter table Table1 alter column username varchar(40)

删除列:

alter table Table1 drop column username

5、按范围查询编号在2到5之间的用户信息

select * from UserValue where UserID between 2 and 5

6、找出表ppp里面mum是最小的值的一条记录

方法1:select top 1 mum from ppp order by mum

方法2:select * from ppp where mum=(select Min(mum) from ppp)

7、复制表

select * into new from old where 1=0 (只复制表结构)

select * into new from old(拷贝数据)

8、在数据表A中,有一个字段LASTUPDATETIME,是最后更新的时间,如果要查最新更新的时间,如何写SQL语句

select * from A where LASTUPDATETIME=(select Max(LASTUPDATETIME)from A)

9、日期字符串转换函数to_char(),to_date()

select to_char(sysdate,'hh:mi:ss')from dual; 结果:10:51:43

select to_date('20080229132545','yyyy-mm-dd hh24:mi:ss')from dual 结果:2008-2-29 13:25:45

10、修改人员信息表(a01)的数据,将联系电话以11开头的人员的学历改为”大专”。

update Employee set E_XueLi ='大专' where E_Tel like'11%'

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: