select * from TblPerson
select * from PhoneNum
select * from PhoneType
select pid,pname,pcellPhone,ptname,ptid from PhoneNum pn inner join PhoneType as pt on pn.pTypeId=pt.ptid
select * from [user]
create table [user]
(
uId int identity(1,1) primary key,
name varchar(50),
level int --1骨灰 2大蝦 3菜鳥
)
insert into [user] (name,level) values('犀利哥',1)
insert into [user] (name,level) values('小月月',2)
insert into [user] (name,level) values('芙蓉姐姐',3)
select
*,
頭銜='菜鳥'
from [user]
--相當於是c#中的if-else
--要求 then 后面的數據類型必須一致
select
*,
頭銜=case
when [level]=1 then '菜鳥'
when [level]=2 then '老鳥'
when [level]=3 then '大師'
else '骨灰級大師'
end
from [user]
--相當於C#中的switch
select
*,
頭銜=case [level]
when 1 then '菜鳥'
when 2 then '老鳥'
when 3 then '大師'
else '骨灰級大師'
end
from [user]
select * from TblScore
select
tscoreId,
tsid,
tenglish,
等級=case
when tenglish>=95 then '優'
when tenglish>=80 then '良'
when tenglish>=70 then '中'
else '差'
end
from TblScore
>=95 優
>=80 良
>=70 中
小於70 差
select
*,
是否及格=case
when tEnglish>=60 and tMath>=60 then '及格'
else '不及格'
end
from TblScore
--------------
select * from TestA
create table TestA
(
A int,
B int,
C int
)
insert into TestA values(10,20,30)
insert into TestA values(20,30,10)
insert into TestA values(30,10,20)
insert into TestA values(10,20,30)
select * from TestA
--表中有A B C三列,用SQL語句實現:
--當A列大於B列時選擇A列否則選擇B列,當B列大於C列時選擇B列否則選擇C列。
select
X=case
when A>B then A
else B
end,
Y=case
when B>C then B
else C
end
from TestA
--在訂單表中,統計每個銷售員的總銷售金額,列出銷售員名、總銷售金額、稱號(>6000金牌,>5500銀牌,>4500銅牌,否則普通)
select * from MyOrders
select
銷售員,
總金額=sum(銷售價格*銷售數量),
稱號=case
when sum(銷售價格*銷售數量)>6000 then '金牌'
when sum(銷售價格*銷售數量)>5500 then '銀牌'
when sum(銷售價格*銷售數量)>4500 then '銅牌'
else '普通'
end
from MyOrders
group by 銷售員
------------------------------
select * from test
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)
單號 收入 支出
Rk1 10 0
Rk2 20 0
Rk3 0 30
Rk4 0 10
select
單號=number,
收入=case
when amount>=0 then amount
else 0
end,
支出=case
when amount>=0 then 0
else abs(amount)
end
from test
--------------------------------------------------------------------
select *from Teamscore
select
球隊名稱=teamName,
勝=case
when gameResult='勝' then 1
else 0
end,
負=case
when gameResult='負' then 1
else 0
end
from TeamScore
--------------
select
球隊名稱=teamName,
勝=sum(case
when gameResult='勝' then 1
else 0
end),
負=sum(case
when gameResult='負' then 1
else 0
end)
from TeamScore
group by teamName
---------------------------------------------------------
select
球隊名稱=teamName,
勝=case
when gameResult='勝' then '勝'
else null
end,
負=case
when gameResult='負' then '負'
else null
end
from TeamScore
select
球隊名稱=teamName,
勝=count(case
when gameResult='勝' then '勝'
else null
end),
負=count(case
when gameResult='負' then '負'
else null
end)
from TeamScore
group by teamName
-----------------------
select * from NBAScore
select
teamName,
第1賽季=max(case
when seasonName='第1賽季' then score
else null
end),
第2賽季=max(case
when seasonName='第2賽季' then score
else null
end),
第3賽季=max(case
when seasonName='第3賽季' then score
else null
end)
from NBAScore
group by teamName
select * from StudentScore
select
studentId,
語文=max(case
when courseName='語文' then score
else null
end),
數學=max(case
when courseName='數學' then score
else null
end),
英語=max(case
when courseName='英語' then score
else null
end)
from StudentScore
group by studentId
select * from MyOrders
--------------索引--------------
--1.索引的目的:提高查詢效率
--2.索引分兩種:
--2.1聚集索引(物理),一個表中只能有一個聚集索引。
--2.2非聚集索引(邏輯),一個表中可以有多個非聚集索引。
--3.增加索引后,會增加額外的存儲空間。同時降低了增加新紀錄,修改,刪除的效率。
啊
吧
a
aa
b
一
--------------------索引---------------------------
select c3,c4 from TestIndex1002
where c4>800 and c4<1000 order by c4 asc
create clustered index IXc4 on TestIndex1002(c4)
drop index TestIndex1002.IXc4
------------------------
--3.將where條件變為c3='backuplsnparamorhinttext',再測試select c3,c4 from TestIndex1002 where c3='backuplsnparamorhinttext',雖然有一個聚集索引但是這個查詢的where條件並沒有充分利用該索引的優點,索引性能提升並不大。
select c3,c4 from TestIndex1002
where c3 in('backuplsnparamorhinttext','AddresscellPhone')
order by c3 desc
create nonclustered index IXc3 on TestIndex1002(c3)
drop index TestIndex1002.IXc3
CREATE CLUSTERED INDEX [_dta_index_TestIndex1002_c_5_821577965__K3] ON [dbo].[TestIndex1002]
(
[c3] ASC
)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [_dta_index_TestIndex1002_5_821577965__K4_3] ON [dbo].[TestIndex1002]
(
[c4] ASC
)
INCLUDE ( [c3]) WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
----------------------子查詢-----------------------------
--查詢'高二二班'的所有學生
select * from TblClass
select * from TblStudent where tSClassId=5
select * from TblStudent where tSClassId=
(select tClassId from TblClass where tClassName='高二二班')
select *
from TblStudent as ts
where
exists(
select * from TblClass as tc
where ts.tSClassId=tc.tClassId and tc.tClassName='高二二班'
)
exists('fdsfsdf')
SELECT * FROM (SELECT * FROM student where sAge<30) as t
select
(select max(tenglish) from tblscore),
(select min(tenglish) from tblscore),
(select avg(tenglish) from tblscore)
SELECT
1 AS f1,
2 as f2,
(SELECT tenglish FROM tblscore) as f3
select * from TblStudent
select * from TblStudent where tsclassid in (select tclassid from TblClass)
select * from student
where sClassId in
(select cId from class where cName='高一一班' or cName='高二一班')
Select * from student
where exists(
select * from class where (cName='高一一班' or cName='高二二班') and class.cid=student.sclassid
)
select studentId,english from score where studentId in
(select sId from student where sName='劉備' or sName = '關羽' or sName='張飛')
delete from student where sId in
(select sId from student where sName='劉備' or sName = '關羽' or sName='張飛')
----------------分頁查詢---------------------------------
----------使用top實現分頁-----------------------------------
--要分頁查詢,或者分頁顯示,首先要確定按照什么排序,然后才能確定哪些記錄應該在第一頁,哪些記錄應該在第二頁。
select * from Customers
--每頁顯示7條數
--第1頁
select top 7 * from Customers order by CustomerID asc
--請查詢出前2頁的數據
select top (7*2) * from Customers order by CustomerID asc
--第2頁,思路:
--2.1先查詢出(2-1)頁的數據的CustomerID
select top 7 * from Customers where CustomerID not in
(select top (7*(2-1)) CustomerID from Customers order by CustomerID asc)
order by CustomerID asc
--第5頁
--查詢出前4也的數據的Id
select top 7 * from Customers where CustomerID not in
(select top (7*(5-1)) CustomerID from Customers order by CustomerID asc)
order by CustomerID asc
select top 7 * from Customers order by CustomerID asc
--------------使用row_number()實現分頁---------------------------------------
--1.為數據排序,然后編號。
select *,Rn=row_number() over(order by CustomerID asc) from Customers
--2.根據用戶要查看的每頁記錄條數,以及要查看第幾頁。確定應該查詢第幾條到第幾條
--每頁顯示7條,要查看第8頁
--從 (8-1)*7+1 ... 8*7
select *
from (select *,Rn=row_number() over(order by CustomerID asc) from Customers) as t
where t.Rn between (8-1)*7+1 and 8*7
---作業:查詢MyStudent表
select * from MyStudent
select * from TblStudent
select * from Tblclass
--案例1:查詢所有學生的姓名、年齡及所在班級
--TblStudent,TblClass
select
t1.tsname,
t1.tsage,
t2.tclassName
from TblStudent as t1
inner join TblClass as t2 on t1.tsclassid=t2.tclassId
--案例2:查詢年齡超過20歲的學生的姓名、年齡及所在班級
--TblStudent,TblClass
select
t1.tsname,
t1.tsage,
t2.tclassName
from TblStudent as t1
inner join TblClass as t2 on t1.tsclassid=t2.tclassId
where t1.tsage>20
--案例3:查詢學生姓名、年齡、班級及成績
--TblStudent,TblClass,TblScore
select
t1.tsname,
t1.tsage,
t2.tclassName,
t3.tEnglish,
t3.tMath
from TblStudent as t1
inner join TblClass as t2 on t1.tsclassid=t2.tclassId
inner join TblScore as t3 on t1.tsid=t3.tsid
select * from TblScore