SQL Server 進階sql總結

來源:互聯網
上載者:User

標籤:

----------------------------------------------------------------------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總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.