----------------------------------------------------------------------Select--------------------------------------------------------------------------------------------------------------------

1、查询表中所有的数据

-----------select * from 表名

-----------select all  id,name from 表名

-----------select id,name  from 表名

2、查询表中完全匹配的数据

-----------select *  from 表A,表B where 表A.id=表B.id

---------- select  *  from 表A inner join  表B on 表A.id=表B.id

3、数据过滤【完全过滤/字段过滤】

----------- select  distinct *  from 表A

-----------select  distinct(字段) from 表A

4、数据统计

-----------select count(*) from 表A

-----------select count(1) from 表A

-----------select count(字段) from 表A

-----------select count(distinct 字段) from 表A

5、查询前N条数据

-----------select top n *  from 表A

-----------select top (n) * from c order by id desc

6、查询中的in和not in 与exists和 not exists

----------select * from 表A where not exists (select * from  表B where id = 表A.id )

----------select * from 表A where  exists (select * from  表B where id = 表A.id )

---------select * from 表A where id in  (select id from  表B)

---------select * from 表A where id  not  in  (select id from  表B)

7、数据库中and与or的用法

--------select * from 表A where 字段A='' and 字段B=''

--------select * from 表A where 字段A='' or 字段B=''

8、union和union all进行并集运算

--------select id, name from 表A where 字段 like '%' union select id, 字段 from 表A where id = 4 ---------------------并集不重复

--------select * from 表A where name like '%' union all select * from 表A---------------------并集重复

---------select * from 表A where 字段 like '%' intersect select * from 表A--------------------------交集(相同部分)

----------select * from 表A where 字段 like '%' except select * from 表A where 字段 like '%' -------------除去(相同部分)

9、聚合函数

---------select max(字段) 表A

---------select min(字段) 表A

---------select count(字段) 表A

---------select avg(字段) 表A

---------select sum(字段) 表A

---------select var(字段) 表A

10、日期函数

-------select dateAdd(day, 3, getDate())

-------select dateAdd(year, 3, getDate())

-------select dateAdd(hour, 3, getDate())

---------------------计算返回数据----------------

----------select dateDiff(day, '2016-01-01', getDate())

----------select dateDiff(second, '2016-01-01 00:00:00', getDate())

----------select dateDiff(hour, '2016-01-01 00:00:00', getDate())

----------select dateName(month, getDate())

----------select dateName(minute, getDate())

----------select dateName(weekday, getDate())

---------select datePart(month, getDate())

---------select datePart(weekday, getDate())

---------select datePart(second, getDate())

---------select day(getDate())

---------select day('2016-01-01')

--------select month(getDate())

--------select month('2016-01-01')

--------select year(getDate())

--------select year('2016-01-01')

--------select getDate()

--------select getUTCDate()

11、数学函数

-------select pi()

-------select rand(100), rand(50), rand(), rand()

-------select round(rand(), 3), round(rand(100), 5)

-------select round(123.456, 2), round(254.124, -2)

-------select round(123.4567, 1, 2)

12、字符串

------select len(字符串)

------select reverse(字符串)

------select left(字符串, 4)

------select right(字符串, 4)

------select lower('abc'), lower('ABC')

------select upper('ABc'), upper('abc')

13、创建数据库

-------create database 库名称

14、删除数据库

------drop  database  数据库名称

15、备份sql

-----use master exes sp_addumpdevice '数据库' ,'备份名称','备份地址'

16、开始备份

-----  backup  database pubs to '备份名称'

17、创建表

------create  table 表名称

18、删除表

------drop  table 表名称

19、增加列

------alter table 表名称 add column col 列名称

20、创建视图

-------create  view  视图名称

21、删除视图

-----drop  view 视图名称

22、复制表数据

-------select  *  into 表B from 表A

------select top 0 into 表B from 表A

23、between的用法

------select *  from 表A  where time between  time1 and time2

-----select  *  from  表A where id  between  id1  and id2

SQL Server 高级sql总结的更多相关文章

  1. SQL Server 高级SQL

    查询view 的列和列数据类型 SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'vi ...

  2. .NET/ASP.NET/C#/WCF/SQL Server/My SQL/Java/JSP/JDBC/Spring/Spring MVC/PHP/Python/Ruby/Shell/Agile/CSS/HTML/HTTP/Unix/Linux大量PDF书籍/电子书籍下载, Effective Java 下载

    223本电子书籍,囊括了.NET/ASP.NET/C#/WCF/SQL Server/My SQL/Java/JSP/JDBC/Spring/Spring MVC/PHP/Python/Shell/A ...

  3. SQL Server经典sql语句大全(转)

    一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...

  4. PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法

    逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...

  5. [Oracle][ODBC SQL Server Driver][SQL Server]对象名 'RECOVER.HS_TRANSACTION_LOG' 无效(转)

    原帖由 qingyun 于 2010-6-21 15:44 发表 在写pl/sql的时候,有个很重要的注意点:比如:begin  update  某个sqlserver的表@dblink名字 .... ...

  6. (转)[SQL Server] 动态sql给变量赋值(或返回值给变量)

    本文转载自:http://blog.csdn.net/xiaoxu0123/article/details/5684680 [SQL Server] 动态sql给变量赋值(或返回值给变量) decla ...

  7. SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件

    原文:SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件 从2005开始,就引入了"数据库邮件"功能.并且取代SQLMail.原有SQLMail ...

  8. SQL Server审计功能入门:SQL Server审核 (SQL Server Audit)

    原文:SQL Server审计功能入门:SQL Server审核 (SQL Server Audit) 介绍 Audit是SQL Server 2008之后才有的功能,它能告诉你"谁什么时候 ...

  9. sql server 与 sql server compact 互相数据导入

    从SQL Server 导出数据到 Sql Compact 使用 Sql Server Compact Tool box 从SQL Server Comapct 导出数据到  Sql Server 使 ...

随机推荐

  1. 解决springmvc报No converter found for return value of type: class java.util.ArrayList问题

    一.背景 最近闲来无事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的环境(搭建步骤会在以后博客中给出),结果运行程序时,适用@ResponseBody注解进行返回Lis ...

  2. centos6.5安装elasticsearch

    java下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htmles下载地址 : ...

  3. [转]IIS6.0迁移至IIS7.0

    原文地址:http://www.splaybow.com/post/iis-6.0-7.0.html 公司的项目需要迁移到IIS7的目标机器中 在此做记录 原来server 2003系统 迁到2008 ...

  4. 在C#中实现Json的序列化与反序列化

    第一种方式利用 JavaScriptSerializer [对应的Assembly 为 System.Web.Extensions.dll] 进行处理: public static class Kas ...

  5. 学习微信小程序之css3display

    一display diaplay改变标签的模式,行内装块级(block),块级转行内(inline) 通过设置display为none,可以 让整个标签在页内移除掉 设置visibility为hidd ...

  6. Winform MDI窗体容器、权限、简单通讯

    MDI窗体容器: 一般来说,窗体是顶级容器,不允许放在其他任何容器内,但是如果将某个窗体的IsMdiContainer属性设置为True,那此窗体就会成为窗体容器,可以在其中放入其他窗体 在内部的窗体 ...

  7. 【iOS [[UIApplication sharedApplication] delegate]】运用

    之前想要拿到app的窗口,我们通常的写法是: [UIApplication sharedApplication].keyWindow 这种写法之前一直也觉得是正确的,没什么问题,而且网上大多数的博客或 ...

  8. python调用c\c++

    前言 python 这门语言,凭借着其极高的易学易用易读性和丰富的扩展带来的学习友好性和项目友好性,近年来迅速成为了越来越多的人们的首选.然而一旦拿python与传统的编程语言(C/C++)如来比较的 ...

  9. 【vuejs小项目——vuejs2.0版本】单页面搭建

    http://router.vuejs.org/zh-cn/essentials/nested-routes.html 使用嵌套路由开发,这里会出错主要把Vue.use(VueRouter);要进行引 ...

  10. 老生长谈的$.extend()方法

    jq的extend()是jq插件扩展很重要的部分,到这里证明是可以自己在jq的基础上,分为两种方法去扩展或开发,为jq本身添加一个方法,可以理解成扩展静态方法和自定义方法. 今天有看到一篇帖子,对这部 ...