case 嵌套查詢(xún)與連接查詢(xún)你需要懂得
1、Case 子查詢(xún)連接查詢(xún)
select * from score
create database demo
use demo
create table [user]
(
[uId] int identity( 1 ,1 ) primary key,
[name] varchar ( 50),
[level] int --1骨灰大蝦菜鳥(niǎo)
)
insert into [user] ( name, level ) values (' 犀利哥 ', 1 )
insert into [user] ( name, level ) values (' 小月月 ', 2 )
insert into [user] ( name, level ) values (' 芙蓉姐姐 ', 3 )
--case end 單值判斷 相當(dāng)于 switch case
--then 后面的返回值類(lèi)型必須一致
select [name] ,
case [level]
when 1 then '骨灰 '
when 2 then '大蝦 '
when 3 then '菜鳥(niǎo) '
end as '等級(jí) '
from [user]
use MySchool
select * from score
--case end 第二種用法,相當(dāng)于多重 if 語(yǔ)句
select studentId ,
case
when english >=90 then ' 優(yōu) '
when english >=80 and english <90 then ' 良 '
when english >=70 and english < 80 then ' 中 '
when english >= 60 and english < 70 then ' 可 '
else ' 差 '
end as '成績(jī) '
from score
order by english
-- 表中有A B C 三列 ,用 SQL 語(yǔ)句實(shí)現(xiàn):當(dāng) A列大于 B 列時(shí)選擇A 列否則選擇 B 列,當(dāng)B 列大于 C列時(shí)選擇 B 列否則選擇 C列。
select
case
when a > b then a
else b
end ,
case
when b > c then b
else c
end
from T
-- 練習(xí)
create table test
(
number varchar ( 10),
amount int
)
insert into test( number ,amount ) values ( 'RK1', 10 )
insert into test( number ,amount ) values ( 'RK2', 20 )
insert into test( number ,amount ) values ( 'RK3',- 30 )
insert into test( number ,amount ) values ( 'RK4',- 10 )
select number ,
case
when amount > 0 then amount
else 0
end as '收入 ' ,
case
when amount < 0 then abs ( amount)
else 0
end as '支出 '
from test
--結(jié)果如下
-- 有一張表student0 ,記錄學(xué)生成績(jī)
use demo
CREATE TABLE student0 ( name nvarchar (10 ), subject nvarchar (10 ), result int )
INSERT INTO student0 VALUES (' 張三 ', ' 語(yǔ)文' , 80)
INSERT INTO student0 VALUES (' 張三 ', ' 數(shù)學(xué)' , 90)
INSERT INTO student0 VALUES (' 張三 ', ' 物理' , 85)
INSERT INTO student0 VALUES (' 李四 ', ' 語(yǔ)文' , 85)
INSERT INTO student0 VALUES (' 李四 ', ' 數(shù)學(xué)' , 92)
INSERT INTO student0 VALUES (' 李四 ', ' 物理' ,null)
select * from student0
select [name] ,
isnull (sum ( case subject
when ' 語(yǔ)文 ' then result
end ),0 ) as '語(yǔ)文 ' ,
isnull (sum ( case subject
when ' 數(shù)學(xué) ' then result
end ),0 ) as '數(shù)學(xué) ' ,
isnull (sum ( case subject
when ' 物理 ' then result
end ),0 ) as '物理 '
from student0
group by [name]

-- 子查詢(xún)將一個(gè)查詢(xún)語(yǔ)句做為一個(gè)結(jié)果集供其他 SQL 語(yǔ)句使用,就像使用普通的表一樣,
-- 被當(dāng)作結(jié)果集的查詢(xún)語(yǔ)句被稱(chēng)為子查詢(xún)。所有可以使用表的地方幾乎都可以使用子查詢(xún)來(lái)代替。
use myschool
select sName from ( select * from student ) as t
select 1,( select sum ( english) from score ) as ' 和 ',( select avg ( sAge) from student ) as ' 平均年齡 '
-- 查詢(xún)高一一班所有的學(xué)生
select * from student where sClassId =
( select cId from class where cName = '高一一班 ' )
-- 查詢(xún)高一一班 高二一班所有的學(xué)生
-- 子查詢(xún)返回的值不止一個(gè)。當(dāng)子查詢(xún)跟隨在 = 、!= 、 <、 <= 、> 、 >= 之后
-- 子查詢(xún)跟在比較運(yùn)算符之后,要求子查詢(xún)只返回一個(gè)值
-- 如果子查詢(xún)是多行單列的子查詢(xún),這樣的子查詢(xún)的結(jié)果集其實(shí)是一個(gè)集合??梢允褂?in 關(guān)鍵字代替 =號(hào)
select * from student where sClassId =
( select cId from class where cName in ( '高一一班 ' ,' 高二一班 '))
select * from student where sClassId in
( select cId from class where cName in ( '高一一班 ' ,' 高二一班 '))
-- 查詢(xún)劉關(guān)張的成績(jī)
select * from score where studentId in
( select sId from student where sName in ( '劉備 ' ,' 關(guān)羽 ', ' 張飛' ))
-- 刪除劉關(guān)張
delete from score where studentId in
( select sId from student where sName in ( '劉備 ' ,' 關(guān)羽 ', ' 張飛' ))
-- 實(shí)現(xiàn)分頁(yè)
-- 最近入學(xué)的個(gè)學(xué)生
select top 3 * from student
order by sId desc
-- 查詢(xún)第到個(gè)學(xué)生
select top 3 * from student
where sId not in ( select top 3 sId from student order by sId desc)
order by sId desc
-- 查詢(xún)到的學(xué)生
select top 3 * from student
where sId not in ( select top 6 sId from student order by sId desc)
order by sId desc
-- 上面是sql 2000 以前的實(shí)現(xiàn)方式。 SQLServer2005 后增加了Row_Number 函數(shù)簡(jiǎn)化實(shí)現(xiàn)。
--sql 2005 中的分頁(yè)
select * from
( select row_number () over (order by sId desc ) as num,* from student ) as t
where num between 1 and 3
select * from
( select row_number () over (order by sId desc ) as num,* from student ) as t
where num between 4 and 6
select * from
( select row_number () over (order by sId desc ) as num,* from student ) as t
where num between 7 and 9
select * from
( select row_number () over (order by sId desc ) as num,* from student ) as t
where num between 3 *( 3- 1 ) + 1 and 3 *3
-- 表連接
-- 交叉連接cross join
select * from student
cross join class
-- 內(nèi)連接inner join...on...
select * from student
inner join class on sClassId = cId
select * from class
-- 查詢(xún)所有學(xué)生的姓名、年齡及所在班級(jí)
select sName , sAge, cName ,sSex from student
inner join class on sClassId = cId
where sSex =' 女 '
-- 查詢(xún)年齡超過(guò)歲的學(xué)生的姓名、年齡及所在班級(jí)
select sName , sAge, cName from class
inner join student on sClassId = cId
where sAge > 20
-- 外連接
--left join...on...
select sName , sAge, cName from class
相關(guān)文章
SQL Server數(shù)據(jù)庫(kù)之備份和恢復(fù)數(shù)據(jù)庫(kù)
在一些對(duì)數(shù)據(jù)可靠性要求很高的行業(yè),若發(fā)生意外停機(jī)或數(shù)據(jù)丟失,其損失是十分慘重的,因此,本文詳細(xì)介紹了數(shù)據(jù)庫(kù)備份和恢復(fù)數(shù)據(jù)庫(kù)方法,感興趣的同學(xué)可以借鑒一下2023-03-03
delete誤刪數(shù)據(jù)使用SCN號(hào)恢復(fù)(推薦)
這篇文章主要介紹了使用scn號(hào)恢復(fù)誤刪數(shù)據(jù)問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
SQL Server多表查詢(xún)優(yōu)化方案集錦
本文我們主要對(duì)SQL Server多表查詢(xún)的優(yōu)化方案進(jìn)行了總結(jié),并給出了實(shí)際的例子進(jìn)行性能與效率的對(duì)比,需要的朋友可以參考下2015-08-08
sqlserver中在指定數(shù)據(jù)庫(kù)的所有表的所有列中搜索給定的值
最近因ERP項(xiàng)目,我們需要知道前臺(tái)數(shù)據(jù)導(dǎo)入功能Application操作的導(dǎo)入字段都寫(xiě)入到了后臺(tái)數(shù)據(jù)庫(kù)哪些表的哪些列2011-09-09
獲取數(shù)據(jù)庫(kù)中兩個(gè)時(shí)間字段的相差天數(shù)及ABS/DATEDIFF函數(shù)應(yīng)用
本文將詳細(xì)介紹獲取數(shù)據(jù)庫(kù)中兩個(gè)時(shí)間字段的相差天數(shù)及ABS/DATEDIFF函數(shù)應(yīng)用感興趣的朋友可以了解下哦,希望本文對(duì)你鞏固ABS/DATEDIFF函數(shù)有所幫助2013-01-01
SQL?Server基礎(chǔ)教程之游標(biāo)(Cursor)
這篇文章主要給大家介紹了關(guān)于SQL?Server基礎(chǔ)教程之游標(biāo)(Cursor)的相關(guān)資料,游標(biāo)是SQL Server的一種數(shù)據(jù)訪問(wèn)機(jī)制,它允許用戶(hù)訪問(wèn)單獨(dú)的數(shù)據(jù)行,需要的朋友可以參考下2023-11-11
SQL Server 數(shù)據(jù)庫(kù)中的收縮數(shù)據(jù)庫(kù)和文件操作
收縮數(shù)據(jù)文件通過(guò)將數(shù)據(jù)頁(yè)從文件末尾移動(dòng)到更靠近文件開(kāi)頭的未占用的空間來(lái)恢復(fù)空間,在文件末尾創(chuàng)建足夠的空間后,可取消對(duì)文件末尾的數(shù)據(jù)頁(yè)的分配并將它們返回給文件系統(tǒng),本文給大家介紹SQL Server 數(shù)據(jù)庫(kù)中的收縮數(shù)據(jù)庫(kù)和文件的相關(guān)知識(shí),一起看看吧2023-07-07
sql更新語(yǔ)句中update set from用法實(shí)現(xiàn)
本文主要介紹了sql更新語(yǔ)句中update set from用法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

