转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
SQL常用的精典语句         ★★★★

SQL常用的精典语句

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1528 更新时间:2009/6/9 2:36:08

1:说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
方法一:select * into b from a where 1<>1
方法二:select top 0 * into b from a
注意:复制的新表中的所有字段都将没有默认值,即使源表中有设默认值

2:说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..


3:说明:显示文章、提交人和最后回复时间
select a.title,a.username,b.adddate from a,(select max(adddate) adddate from table,a where table.title=a.title) b


4:说明:两张关联表,删除主表中在副表中没有的信息
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
注意:
若将
select * from table2 where table1.field1=table2.field1
改为
select * from table1,table2 where table1.field1=table2.field1
多了一个table1
则删除不成功

5:说明:日程安排提前五分钟提醒
select * from 日程安排 where datediff(mi,开始时间,getdate())>5

说明:一条sql 语句搞定数据库分页
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段

6:说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)


7:说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
(select a from tableA ) except (select a from tableB) except (select a from tableC)


8:说明:随机取出10条数据
select top 10 * from tablename order by newid()


9:说明:随机选择记录
select newid()


10:说明:删除重复记录
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)


11:说明:列出数据库里所有的表名
select name from sysobjects where type='U'


12:说明:列出表里的所有的
select name from syscolumns where id=object_id('TableName')
使用SQL语句 用...代替过长的字符串显示
语法:
SQL数据库:select case when len(field)>10 then left(field,10)+'...' else field end as news_name,news_id from tablename
Access数据库:SELECT iif(len(field)>2,left(field,2)+'...',field) FROM tablename;
--获得当月最后一天,时间全是0,如:2007-09-30 00:00:00.000
select dateadd(dd,-1,dateadd(mm,datediff(mm,0,dateadd(mm,1,getdate())),0))
--获得当月最后一天,时间为当前时间,如:2007-09-30 12:07:37.030
select dateadd(dd,-1,dateadd(mm,1,getdate()-day(getdate())+1))
--获得当月天数,如:30
select datediff(dd,getdate()-day(getdate())+1,dateadd(mm,1,getdate()-day(getdate())+1))

 


[C语言系列]C#中combobox 控件属性、事件、方法(部分常用)  [网站推广]总结8种常用的网站推广方法
[办公软件]PowerPoint操作常用快捷键尽收眼底  [办公软件]EXCEL常用函数及功能介绍一
[办公软件]Excel常见错误信息以及解决方法  [网络安全]黑客入侵计算机常用的四种手法及防范措施
[网络安全]三大绝招防御SQL注入式攻击  [Sql Server]不用SqlTransaction执行数据库事务处理
[Sql Server]SQL各种常用数值(数据)运算函数详解  [Sql Server]Sql Server如何附加mdf数据库到企业管理器
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台