T SQL基礎及T SQL分頁查詢

2021-10-06 02:08:44 字數 3209 閱讀 9699

資料定義語言(ddl,data definition language)

資料操縱語言(dml,data manipulation language)

資料控制語言(dcl,data control language)

核心作用主要用作用快速的條件查詢

運算子作用

=判斷相等

>

大於<

小於》=

大於等於

<=

小於等於

!= <>

不等於!

非運算子

作用all

如果乙個比較集中全部都是true,則值為true

any如果乙個比較集中任何乙個為true,則值為true

and如果兩個布林表示式均為true,則值為true

or如果任何乙個布林表示式是true,則值為true

between

如果運算元是在某個範圍內(可取上下限),則值為true

like

(模糊查詢)如果運算元匹配某個模式的話,則值為true

in如果運算元與乙個表示式列表中的某個相等的話,則值為true

not對任何其他布林運算子的值取反

some

如果乙個比較集中的某些為true的話,則值為true

exists

如果子查詢包含任何行,則值為true

在搜尋資料庫中的資料時,sql 萬用字元可以替代乙個或多個字元。可以理解為和c#中學習過的正則的元字元類似

萬用字元描述

%替代乙個或多個字元

_僅替代乙個字元

[a,b,d],[a-d]

字元列中的任何單一字元

[ ^a,b,c] 或者 [!b]

不在字元列中的任何單一字元

對查詢結果的字段重新命名

select 欄位1名 as '重新命名1',欄位2名 as '重新命名2',.... from 表名

select stuid as '學號',subid as '科目id',score as '分數' from score

內聯接查詢可以為將資料庫中多張表通過其內部關係進行查詢,得到一張新的有用的"表",這個新錶是內聯接查詢的結果並不存在

注意:多張表要保證表與表之間的字段有某種特殊意義的聯絡

select sname as '姓名',subname as '科目',score as '分數' from score join student on score.stuid=student.sid join subject on score.subid=subject.subid
排序查詢肯定是先排序再查詢

將查詢結果按照某個指定欄位以公升序(例如:數字按照從小到大)、降序這兩種方式進行重新排列顯示。

select 《字段》 from 表名 [where條件] order by 《指定列名》 asc/ desc,《指定列名》 asc/ desc
公升序:使用關鍵字asc,如果在查詢語句中order by排序之後未宣告任何關鍵字則預設是公升序

降序:使用關鍵字desc

select sname as '姓名',subname as '科目',score as '分數' from score join student on score.stuid=student.sid join subject on score.subid=subject.subid where subname='c#'order by score desc
注意:排序查詢語句中的公升序或降序不是僅有乙個字段

使用top可以從結果中查詢出指定的前多少行

select top(數量) 《列名》 from 表名
從表中查詢前多少行,由括號中的數量決定

select top(3) sname as '姓名',subname as '科目',score as '分數' from score join student on score.stuid=student.sid join subject on score.subid=subject.subid where subname='c#'
針對於目標資料量龐大的查詢結果,為了能夠快速查詢

分頁查詢有助於應用程式對資料的乙個顯示

select top(5) * from studentinfo where stuid not in(select top(5)stuid from studentinfo order by stuid) order by stuid
select top(每頁的資料量) * from 表名 where 標識列 not in(select top(已經過去的頁數)標識列 from 表名 order by 標識列) order by 標識列
select top(5) * from studentinfo where stuid in

(select top(5) stuid from studentinfo where stuid in

(select top(10) stuid from studentinfo order by stuid)order by stuid desc)order by stuid asc

select * from 表名 where 標識列 in (select top(每頁的資料) 標識列 from 表名 where 標識列 in (select top(當前頁在內的總資料) 標識列 from 表名 order by 標識列)order by 標識列 desc)order by 標識列 asc
注意:

1.在子連線查詢中如果有多個排序查詢,務必要注意子查詢語句如果要使 用order by則必須要使用top關鍵字

2. 使用in語句進行分頁前提是必須要計算好最後一頁的資料

使用t-sql語句查詢出student表中的第3-第8條資料(標識列不連續)

實現思路:

1.首先查出來前兩條記錄中的標識列資料

2.然後查詢從第3條資料之後剩餘的所有資料

3.最後在剩餘的資料中取前5條資料

select top(6)* from teacher where tid not in (select top(2) tid from teacher order by tid) order by tid

T SQL程式設計基礎

t sql程式設計 練習題,計算1 100之間所有奇數的和 思路,1.宣告兩個變數,乙個用於計數1 100,乙個用於累計和 2.迴圈遍歷1 100之間所有的整數 3.if判斷,是否是奇數。i 2 0 4.如果是,加到累積和的變數中去 declare iint 1 declare sum int 0w...

T SQL分頁功能儲存過程

分頁功能儲存過程 alter procedure dbo p splitpagesquery tablesname nvarchar max 表名或檢視名 只能傳單一表名 pknvarchar max 主鍵 可以為空!strsort nvarchar max 排序字段 按照這個欄位對查詢結果進行排序...

T SQL,動態聚合查詢

if exists select table name from information schema.tables where table name accountmessage drop table accountmessage gocreate table accountmessage ffu...