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

SQL 檢視

--檢視是對於一段sql的封裝
--檢視其實就是一張表(真實的,虛擬的表)

語句如下:

create view a
as
select *from stuInfo

查詢 檢視

select *from a;

這樣就省去寫那些程式碼的時間了

create table region(
id varchar(20) primary key, --主鍵,區域編號
name varchar(20) not null, --區域名稱
pid varchar(20) not null --區域附屬編號 0省份
);

insert into region
select 's2309','廣東省','0'union
select 's2098','湖南省' ,'0'union
select 's2033','廣西省' ,'0'union
select 's2034','永州市' ,'s2098'union
select 's2056','長沙市' ,'s2098'union
select 's2012','廣東市' ,'s2309'union
select 's2089','東莞市' ,'s2309' union
select 's2037','懷化市' ,'s2098'


--查詢所有的省分
select *from region where pid='0'

結果如下:

---- 查詢湖南省下素有的市
select *from region where pid='s2098'

結果如下

select *from region where pid in(
select id from region where name ='湖南省'
)

結果如下:

select a.name,count(b.name)
from region a full join region b
on a.id=b.pid where a.name like '%省%'
group by a.name;

結果如下:

-- 查詢性張的人: 張x,張xx,張xxx
select * from stuInfo where stuName like '張%';

-- 查詢性張的人: 張xx
select * from stuInfo where stuName like '張__';

-- 查詢以麗結尾的人: xx麗
select * from stuInfo where stuName like '%麗';

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