1. 程式人生 > 其它 >SQL server(檢視)

SQL server(檢視)

-- 想知道每個學生對應的成績
select * from stulnfo a inner join stumarke b
on a.stuNo=b.stuNo;

--想知道李斯文的成績
select * from stulnfo a inner join stumarke b on 
a.stuno=b.stuno where stuname='李斯文';

---- 檢視
-- 是一種結果的封裝,是一個的【虛擬】的表
-- 作用: 提供一個已經封裝好的共用於查詢
create view view_a as select *from stulnfo

-- 檢視檢視
select * from view_a

-- 建立一個成績表的檢視
create view view_b as
select * from stumarke

-- 建立一個檢視: 聯表的資料
create view view_c as
select a.*, labexam,writtenexam,examon
from stulnfo a full join stumarke b
on a.stuNo=b.stuNo

-- 想知道每個學生對應的成績
select * from view_c

-- 想知道李斯文的成績
select * from view_c where stuname='李斯文'

-- 哪些人沒考試
select * from view_c where examon is null

-- 檢視中顯示的資料: 男女的比列
create view view_d as
select stusex,count(*)as mount
from stulnfo group by stusex

-- 刪除檢視
drop view view_d

-- 內聯
select * from stulnfo a,stumarke b
where a.stuno=b.stuno

-- 查詢性張的人: 張x,張xx,張xxx
select * from stulnfo where stuname like '張%'

-- 查詢性張的人: 張xx
select * from stulnfo where stuname like '張__'

-- 查詢以麗結尾的人: xx麗
select * from stulnfo where stuname like '%麗'

-- 查詢名字帶秋的人: 秋xx,x秋x,xx秋
select * from stulnfo where stuname like '%秋%'


-- 自連線
-- 省份 -> 市區

-- 區域(省份+城市)
create table regoin(
id varchar(20) primary key,
name varchar(20) not null,
pid varchar (20)not null
);
insert into regoin values('43','湖南省','0')
insert into regoin values('11','北京省','0')
insert into regoin values('98','廣東省','0')
insert into regoin values('18','永州市','43')
insert into regoin values('12','懷化市','43')
insert into regoin values('13','常德市','43')
insert into regoin values('15','廣州市','11')
insert into regoin values('16','深圳市','11')

-- 查詢所有的省份
select * from regoin where pid='0'

-- 查詢湖南省下所有的市
select * from regoin where pid=(
select id from regoin where name='湖南省' 
);

-- 聯表查詢
select * from regoin a,regoin b
where a.id=b.pid

-- 查詢每個省份對應的城市的個數
select a.name,count(b.name)
from regoin a full join regoin b
on a.id=b.pid where a.name like '%省%'
group by a.name