一、使用SELECT檢索數(shù)據(jù)
數(shù)據(jù)查詢是SQL語言的中心內(nèi)容,SELECT 語句的作用是讓數(shù)據(jù)庫服務(wù)器根據(jù)客戶要求檢索出所需要的信息資料,并按照規(guī)定的格式進(jìn)行整理,返回給客戶端。
SELECT 語句的基本結(jié)構(gòu)
[WITH<common_tale_exPRession>]SELECT select_list [INTO new_table_name][FROM table_source][where search_condition][GROUP BY group_by_expression][HAVING search_condition][ORDER BY order_expression [ ASC | DESC ]]
WITH子句
WITH子句用于指定臨時(shí)命名的結(jié)果集,這些結(jié)果集成為公用表表達(dá)式(CTE)。該表達(dá)式源自簡單查詢,并且在單條SELECCT、INSERT、UPDATE或DELETE語句的執(zhí)行范圍內(nèi)定義。
use web;with AgeReps(Age,AgeCount) AS ( select Age,count(*) from tt as AgeReports where age is not null group by age)select age,agecount from AgeReps
查詢命名為AgeReps臨時(shí)表中的年齡,年齡總數(shù)。 臨時(shí)表中為年齡分組并顯示年齡數(shù)量。
SELECT ··· FROM 子句
SELECT 表明要讀取信息,F(xiàn)ROM指定要從中獲取數(shù)據(jù)的一個(gè)或多個(gè)表的名稱。
select * from tables /* *查詢?nèi)苛?*/select id,name from tables /* 查詢指定列 */select tables.id,tables.name from tables /*還可以表.列名*//*別名顯示*/select tt.id ID,tt.name 名字,tt.sex 性別,tt.age 年齡 from ttselect ID = tt.id , 名字 = tt.name from ttselect tt.id as ID,tt.name as 名字,tt.sex as 性別,tt.age as 年齡 from tt
INTO 子句
將查詢的結(jié)果插入到新表中
select tt.id,tt.name,tt.age into newTable from tt
WHERE 子句
為搜索追加搜索條件
1.邏輯運(yùn)算符(NOT、AND、OR)
1.1 NOT : 對布爾型輸入取反,使用NOT返回不滿足表達(dá)式的行。
1.2 AND : 組合兩個(gè)布爾表達(dá)式,當(dāng)兩個(gè)表達(dá)式均為true時(shí)返回true。
1.3 OR : 將兩個(gè)條件組合起來,當(dāng)滿足任意一條時(shí)為true。
優(yōu)先順序是 NOT AND OR。
select * from [user] where id=1 and name = 'a' or name='bc' and not name='d'
2.比較運(yùn)算符
= (是否相等)、 <> (是否彼此不等)、 !=(是否彼此不等) 、 >(大于) 、 >=(大于且等于) 、 !>(不大于) 、 < (小于) 、 < = (小于等于) 、 ! < (不小于) 。
select * from [user] where age > 24
LIKE 關(guān)鍵字
select * from tables where name like ' 王%' 查找王開頭的。select * from tables where name like ' 王_' 查找王開頭后面跟一個(gè)字的。select * from tables where name not like ' 王_' 查找不是王開頭后面跟一個(gè)字的。select * from tables where age like ' 2[2-4]' 查找2后面跟2到4之間的。select * from tables where age like ' 2[^2-4]' 查找2后面不再2到4之間的。
BETWEEN 關(guān)鍵字
select * from tables where age between 22 and 24 查找22 到24之間的。select * from tables where age not between 22 and 24 查找不再22到24之間的。
IS (NOT) NULL 關(guān)鍵字
在where子句中不能使用比較運(yùn)算符(=)來對空值進(jìn)行判斷,只能用IS NULL 來對空值進(jìn)行查詢。
select * from tables where age is null 查詢 age為空的。select * from tables where age is not null age 不為空的。
IN關(guān)鍵字
使用IN關(guān)鍵字來指定搜索范圍,是否與子查詢或列表中的值相匹配。
select * from tables where id in ('001','002','003') 查找id范圍在001 002 003中的。select * from tables where id not in ('001','002','003') 查找id范圍不在001 002 003中的。
ALL、SOME、ANY 關(guān)鍵字
ALL:比較標(biāo)量值和單列集中的值,與比較運(yùn)算符和子查詢一起使用。>ALL標(biāo)識(shí)大于條件的每一個(gè)值,大于最大值。
select * from tt where age > all(select age from tt where age = 24) 查詢大于all里面的查詢值。
SOME|ANY:比較標(biāo)量值和單列集中的值,SOME 和 ANY 是等效的,與比較運(yùn)算符和自查詢一起使用。> ANY 表示至少大于條件的一個(gè)值,大于最小值。
select * from tt where age > any(select age from tt where age = 24)
EXISTS 關(guān)鍵字
select id , name from [user] where exists (select null)
GOUP BY 子句
將按照一個(gè)或多個(gè)列或表達(dá)式的值將一組選定行組合成一個(gè)摘要行集,針對每一組返回一行,分組。
select age from tt group by age
HAVING 子句
通常在GOURP BY 子句中使用,在分組中指定條件搜索。
select age from tt group by age having age = 24
ORDER BY 子句
對搜索進(jìn)行排序,除非同時(shí)指定了TOP ,否則ORDER BY 子句在視圖、內(nèi)斂函數(shù)、派生表和子查詢中無效。
select * from tables order by id desc, 按照ID 倒序排序。select * from tables order by id asc 按照ID 升序排序。
COMPUTE 子句
生成合計(jì)作為附加的匯總列出現(xiàn)在結(jié)果集的最后。當(dāng)與BY 一起使用時(shí),COMPUTE子句在結(jié)果集內(nèi)生成控制中斷和小計(jì)。可在統(tǒng)一查詢內(nèi)指定COMPUTE BY 和 COMPUTE。
select * from tables order by sex compute avg(avg)。 按照性別分組查詢,并將平均年齡顯示最后。select * from tables order by sex compute avg(avg) by sex。 按照性別分組查詢,并按照性別分開顯示,顯示出兩組平均年齡。
ALL 關(guān)鍵字
查詢所有記錄。
select all age from tables
DISTINCT 關(guān)鍵字
去掉搜索結(jié)果中重復(fù)的記錄。
select distinct age from tables
TOP 關(guān)鍵字
限制查詢結(jié)果顯示的行數(shù)
select top 5 * from tables.
二、使用UNION合并多個(gè)查詢結(jié)果
表的合并操作將兩個(gè)表的行合并到了一個(gè)表中,且不需要對這些行作任何更改。在構(gòu)造合并查詢時(shí)必須:
1.兩個(gè)select語句選擇列表中的列數(shù)目必須一樣多,而且對應(yīng)位置上的列的數(shù)據(jù)類型必須相同或者兼容。
2.列的名字或者別名是由第一個(gè)select語句的選擇列決定的。
3.可以為每個(gè)select 語句都增加一個(gè)表示行的數(shù)據(jù)來源的表達(dá)式。
4.可以將合并操作作為select into命令的一部分使用,但是info關(guān)聯(lián)必須放在第一個(gè)select語句中。
5.合并操作默認(rèn)情況下去除重復(fù)的行,如果希望返回重復(fù)的行需要使用 all 關(guān)鍵字。
6.用對所有select 語句的合并操作結(jié)果進(jìn)行排序的order by子句,必須放到最后一個(gè)select 后面,但排序列名必須是第一個(gè)select 選擇列表中的列名。
UNION 與 聯(lián)接之間的區(qū)別
在合并中,兩個(gè)表源列的數(shù)量與數(shù)據(jù)類型必須相同,在聯(lián)接中,一個(gè)表的行可能與另外一個(gè)表的行有很大區(qū)別。
在合并中,行的最大數(shù)量是兩個(gè)表的“和”。在聯(lián)接中,行的最大數(shù)量使他們的“乘積”
去重:select id,name from [user] union select id,title from work 結(jié)果為 id name 下面兩個(gè)表的信息
重復(fù):select id,name from [user] union all select id,title from work 包含重復(fù)行
排序:select id,name from [user] union all select id,title from work order by name desc 排序第一表中的列
列數(shù)不同:select id,name,sex from [user] union all select id,title,null from work order by name desc 用NULL填充
子查詢
子查詢是一個(gè)嵌套在select、insert、update或delete語句或其他子查詢中的查詢,返回單個(gè)值。
select * from tables where id not in (1,3,4)。
嵌套查詢
嵌套查詢是指將一個(gè)查詢塊嵌套在另一個(gè)查詢塊的where子句或having短語的條件中查詢。
select * from tables where id in (select typeid from type where id = 86) id范圍在返回結(jié)果中select * from tables where id not in (select typeid from type where id=84) id范圍不在返回結(jié)果中select * from tables where age < some(select avg(avg) from student) 年齡小于平均年齡select * from tables where age <> any(select avg(avg) from student) 年齡不等于平均年齡select * from tables where age <> all(select avg from student where age > 90) 年齡沒有大于90的信息select * from tables where not exists (select id from user where tables.id = user.id) 查詢id不相等的信息
聯(lián)接查詢
水平方向合并兩個(gè)數(shù)據(jù)集合,產(chǎn)生一個(gè)新的結(jié)果集,聯(lián)接條件可以在from或where子句中指定。
內(nèi)部聯(lián)接
內(nèi)部聯(lián)接是從結(jié)果中刪除其他被聯(lián)接表中沒有匹配行的所有行,所以可能會(huì)丟失信息。
select * from [user] inner join [work] on [user].id=work.id 查詢兩表ID相同的信息。
外部聯(lián)接
1.左向外聯(lián)接 left join
如果左表中的某一行在右表中沒有匹配行,則在關(guān)聯(lián)的結(jié)果中,顯示為空值。
select * from [user] left join [work] on [user].id = work.id 顯示所有user信息 若work沒有對應(yīng)信息顯示為NULL
2.右向外聯(lián)接 right join
與左聯(lián)接相反,如果右表中的某一行在左表中沒有匹配行,則顯示為空值。
select * from [user] right join [work] on [user].id = work.id 以work為主體 若user沒有對應(yīng)信息顯示為NULL
3.完整外聯(lián)接 full join
返回左表和右表的所有行,當(dāng)某一行在另一個(gè)表中沒有匹配行時(shí),另一個(gè)表的選擇列將包含空值。
select * from [user] full join [work] on [user].id = work.id 顯示左右全部信息,若沒有就顯示NULL
交叉聯(lián)接 cross join
第一個(gè)表的行數(shù)乘以第二個(gè)表的行數(shù)等于結(jié)果集的大小
select * from [user] cross join work 平均交叉互補(bǔ) 不顯示NULL
多表聯(lián)接
WHERE:select * from table1,table2,table3 where table1.id =table2.id and table2.id = table3.idFORM:select * from table1 join table2 join table3 on table1.id = table2 and table2.id = table3.id
三、使用CASE函數(shù)進(jìn)行查詢
select 數(shù)字 = case /* 別名 */ when id = 10 then '是1哦' /* 如果id=1 則輸出 是1哦 */ when id = 11 then '11哦' when id = 12 then '12哦' else '沒有' /* 否則 輸出 沒有*/ end from [user]/*修改*/update [user] set sex = case when sex='n' then '男' when sex='a' then '女'end
四、函數(shù)
聚合函數(shù)
count(*):返回行數(shù)。count(列名):返回某列的個(gè)數(shù)。avg(列名):返回某列的平均值。max(列名):返回某列的最大值。min(列名):返回某列的最小值。sum(列名):返回某列值的和。
開窗函數(shù)
--使用聚合函數(shù)后,返回結(jié)果只能是一行--使用over() 可以將聚合函數(shù)擴(kuò)展到所有行--語法 聚合函數(shù)() over()select avg(score) from student; --返回一條信息 平均分。select *,avg(score) over() from student; --返回所有數(shù)據(jù)最后加一列平均分
日期時(shí)間函數(shù)
select getDate(); 當(dāng)前系統(tǒng)日期 select dateadd(day,3,getDate()); 加3天 select dateadd(year,3,getDate()); 加年select dateadd(hour,3,getDate()); 加小時(shí)select dateDiff(day,'2013-02-01',getDate()); 返回相差天數(shù)select dateDiff(second,'2013-02-01',getDate()); 返回相差秒數(shù)select dateName(month,getDate()); 返回當(dāng)前月份select dateName(minute,getDate()); 返回當(dāng)前分鐘 select dateName(weekday,getDate()); 返回當(dāng)前星期select day(getDate()); 返回當(dāng)前日期天數(shù)select month(getDate()); 返回當(dāng)前日期月份select year(getDate()); 返回當(dāng)前日期年份
數(shù)字函數(shù)<
新聞熱點(diǎn)
疑難解答
圖片精選