程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> case 嵌套查詢與銜接查詢你須要理解

case 嵌套查詢與銜接查詢你須要理解

編輯:MSSQL

case 嵌套查詢與銜接查詢你須要理解。本站提示廣大學習愛好者:(case 嵌套查詢與銜接查詢你須要理解)文章只能為提供參考,不一定能成為您想要的結果。以下是case 嵌套查詢與銜接查詢你須要理解正文


1、Case 子查詢銜接查詢

select * from score
create database demo
use demo
create table [user]
(
[uId] int identity( 1 ,1 ) primary key,
[name] varchar ( 50),
[level] int --1骨灰年夜蝦菜鳥
)
insert into [user] ( name, level ) values (' 鋒利哥 ', 1 )
insert into [user] ( name, level ) values (' 小月月 ', 2 )
insert into [user] ( name, level ) values (' 芙蓉姐姐 ', 3 )
--case end 單值斷定 相當於 switch case
--then 前面的前往值類型必需分歧
select [name] ,
case [level]
when 1 then '骨灰 '
when 2 then '年夜蝦 '
when 3 then '菜鳥 '
end as '品級 '
from [user]
use MySchool
select * from score
--case end 第二種用法,相當於多重 if 語句
select studentId ,
case
when english >=90 then ' 優 '
when english >=80 and english <90 then ' 良 '
when english >=70 and english < 80 then ' 中 '
when english >= 60 and english < 70 then ' 可 '
else ' 差 '
end as '成就 '
from score
order by english

-- 表中有A B C 三列 ,用 SQL 語句完成:當 A列年夜於 B 列時選擇A 列不然選擇 B 列,當B 列年夜於 C列時選擇 B 列不然選擇 C列。
select
case
when a > b then a
else b
end ,
case
when b > c then b
else c
end
from T

-- 演習
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
--成果以下



-- 有一張表student0 ,記載先生成就
use demo
CREATE TABLE student0 ( name nvarchar (10 ), subject nvarchar (10 ), result int )
INSERT INTO student0 VALUES (' 張三 ', ' 語文' , 80)
INSERT INTO student0 VALUES (' 張三 ', ' 數學' , 90)
INSERT INTO student0 VALUES (' 張三 ', ' 物理' , 85)
INSERT INTO student0 VALUES (' 李四 ', ' 語文' , 85)
INSERT INTO student0 VALUES (' 李四 ', ' 數學' , 92)
INSERT INTO student0 VALUES (' 李四 ', ' 物理' ,null)
select * from student0
select [name] ,
isnull (sum ( case subject
when ' 語文 ' then result
end ),0 ) as '語文 ' ,
isnull (sum ( case subject
when ' 數學 ' then result
end ),0 ) as '數學 ' ,
isnull (sum ( case subject
when ' 物理 ' then result
end ),0 ) as '物理 '
from student0
group by [name]



-- 子查詢將一個查詢語句做為一個成果集供其他 SQL 語句應用,就像應用通俗的表一樣,
-- 被看成成果集的查詢語句被稱為子查詢。一切可使用表的處所簡直都可使用子查詢來取代。
use myschool
select sName from ( select * from student ) as t
select 1,( select sum ( english) from score ) as ' 和 ',( select avg ( sAge) from student ) as ' 均勻年紀 '
-- 查詢高逐個班一切的先生
select * from student where sClassId =
( select cId from class where cName = '高逐個班 ' )
-- 查詢高逐個班 高二一班一切的先生
-- 子查詢前往的值不止一個。當子查詢追隨在 = 、!= 、 <、 <= 、> 、 >= 以後
-- 子查詢跟在比擬運算符以後,請求子查詢只前往一個值
-- 假如子查詢是多行單列的子查詢,如許的子查詢的成果集實際上是一個聚集。可使用 in 症結字取代 =號
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 ( '高逐個班 ' ,' 高二一班 '))
-- 查詢劉關張的成就
select * from score where studentId in
( select sId from student where sName in ( '劉備 ' ,' 關羽 ', ' 張飛' ))
-- 刪除劉關張
delete from score where studentId in
( select sId from student where sName in ( '劉備 ' ,' 關羽 ', ' 張飛' ))

-- 完成分頁
-- 比來退學的個先生
select top 3 * from student
order by sId desc
-- 查詢第到個先生
select top 3 * from student
where sId not in ( select top 3 sId from student order by sId desc)
order by sId desc
-- 查詢到的先生
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 之前的完成方法。 SQLServer2005 後增長了Row_Number 函數簡化完成。
--sql 2005 中的分頁
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
-- 內銜接inner join...on...
select * from student
inner join class on sClassId = cId
select * from class
-- 查詢一切先生的姓名、年紀及地點班級
select sName , sAge, cName ,sSex from student
inner join class on sClassId = cId
where sSex =' 女 '
-- 查詢年紀跨越歲的先生的姓名、年紀及地點班級
select sName , sAge, cName from class
inner join student on sClassId = cId
where sAge > 20
-- 外銜接
--left join...on...
select sName , sAge, cName from class

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved