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