SQL Server 高級sql總結

来源:http://www.cnblogs.com/lqLove/archive/2016/08/24/5803717.html
-Advertisement-
Play Games

Select 1、查詢表中所有的數據 select * from 表名 select all id,name from 表名 select id,name from 表名 2、查詢表中完全匹配的數據 select * from 表A,表B where 表A.id=表B.id select * fro ...


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

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 久等了,近期公司比較忙,學習的時間都沒有啊,到今日才有時間呢!!!好了,下麵就跟著筆者開始配置Hadoop集群吧。 hosts文件和SSH免密碼登錄配置好了之後,現在進入Hadoop安裝目錄,修改一些配置文件,修改配置還是相對簡單的,一下是需要修改的文件內容(當然這裡只是學習時的配置,更加深入的配置 ...
  • 轉自:http://chuansong.me/n/443660447865 PDB 的創建和訪問 在使用 dbca 建庫時,創建資料庫之前,可以保存一下創建腳本,分析其具體執行過程。以自定義方式創建名稱為julia的資料庫為例,其主要腳本 julia.sql 中包含如下腳本調用: 在第一個腳本 Cr ...
  • 資料庫集群 集群的存在意義是為了保證高可用、數據安全、擴展性以及負載均衡。 什麼是集群? 由二台或更多物理上獨立的伺服器共同組成的"虛擬"伺服器稱之為集群伺服器。一項稱做MicroSoft集群服務(MSCS)的微軟服務可對集群伺服器進 行管理。一個SQL Server集群是由二台或更多運行SQL S ...
  • DAC(Dedicated Admin Connection)是SQL Server 2005引入的一個東西,目的是在SQL Server發生嚴重性能問題的時候仍保留有限的資源保證管理員能夠執行一些簡單的命令用於問題診斷、釋放資源、殺死肇事進程等。微軟官方對DAC的說明:使用專用管理員連接。對於DA ...
  • select tpc.cspcode as id , tpc.cspcode||'('||tc.cname||'--'|| DECODE(tsp.cpagedesc,'LENGTH(tsp.cpagedesc)>30','...',tsp.cpagedesc)||')' as text from t ...
  • 隨著社會經濟的發展,特別是互聯網的發展,企業的數據呈現幾何級的增長,大數據在過去的幾年裡一直是一個非常非常火的概念,並且一直在持續。但與此同時,企業對於數據的理解和應用卻大相徑庭。有一部分企業通過數據分析和挖掘能夠降低企業成本,提升業績,但也有一些企業對於數據的分析和應用停留在較淺層次。所以,數據分... ...
  • 需要的jar包 使用ojdbc6.jar 在/META-INF/MANIFEST.MF里可以看到Specification-Version: 4.0 建表 將文件寫入資料庫 將文件從資料庫中讀出 ...
  • 1.去重語句:DELETE FROM `v_klg_item` WHERE id NOT IN (SELECT * FROM (SELECT MAX(id) FROM `v_klg_item` GROUP BY NAME) AS tmp); 2.資料庫不同表之間列的複製語句:INSERT INTO ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...