查詢,聯(lián)合查詢,視圖,子查詢??-- 聯(lián)合查詢

select * from my_class

union -- 默認(rèn)去重

select * from my_class;

select * from my_class

union all -- 不去重

select * from my_class;

select id,c_name,room from my_class

union all -- 不去重

select name,number,idfrom my_student;

-- 需求:男生升序,女生降序(年齡)(

select * from my_studentwhere sex='男' order by age asc limit 9999999)

union(

select *from my_studentwhere sex='女' order by agedesc limit 9999999);

-- 標(biāo)量子查詢

select * from my_studentwhere c_id=(select id from my_classwhere name='python1910');

-- 列子查詢

select * from my_studentwhere c_idin (select id from my_class);

-- any some all

select * from my_studentwhere c_id =any (select id from my_class);

select * from my_studentwhere c_id =some (select id from my_class);

select * from my_studentwhere c_id =all (select id from my_class);

select * from my_studentwhere c_id !=any (select id from my_class);-- 所有結(jié)果(null除外)

select * from my_studentwhere c_id !=some (select id from my_class);-- 所有結(jié)果(null除外)

select * from my_studentwhere c_id !=all (select id from my_class);-- 1,2不等于345(null除外)

select * from my_studentwhere age=(select max(age)from my_student)and height=(select max(height)from my_student);

-- 行子查詢

select * from my_studentwhere (age,height)= (select max(age),max(height)from my_student);

select *from my_studentorder by age desc,heightdesc limit 1;-- 可能查詢結(jié)果不是預(yù)想的

-- 表子查詢

select * from my_studentgroup by c_idorder by heightdesc;

-- 插入學(xué)生

insert into my_studentvalues (null,'bc20200007','小紅','女',25,185,1);

-- 查找每個(gè)班身高最高的學(xué)生

select * from (select * from my_studentorder by heightdesc limit 9999999)as studentgroup by c_id;

--

select exists(select * from my_student);

select exists(select * from my_studentwhere id=100);

-- exists子查詢

select * from my_studentwhere? exists(select * from my_class);-- 判斷括號(hào)里是否成立

select * from my_studentwhere? exists(select * from my_classwhere id=3);

select * from my_studentwhere? exists(select * from my_classwhere id=2);

-- 視圖:?jiǎn)伪?多表

create view my_v1as select *? from my_student;

create view my_v2as select *? from my_class;

create view my_v3as select *? from my_studentas sleft join my_classas con s.c_id=c.id;-- 錯(cuò)誤:id重復(fù)

-- 多表視圖

create view my_v3as select s.*,c.name as c_name,c.room from my_studentas sjoin my_classas con s.c_id=c.id;

show tables;

-- 查看視圖創(chuàng)建語句

show create view my_v3\G

-- 視圖的使用

select *? from my_v1;

select *? from my_v2;

select *? from my_v3;

-- 修改視圖

alter view my_v1as select id,name,age,sex,height,c_idfrom my_student;

-- 刪除視圖

create view my_v4as select * from my_student;

drop view my_v4;

-- 多表視圖插入數(shù)據(jù) : 不能插入數(shù)據(jù)

insert into my_v3values (null,'bc20200008','張三豐','男',150,180,3,'python1910','a204');

-- 將學(xué)生表的學(xué)號(hào)字段設(shè)置成不能為空

alter table my_studentmodify number char(10)not null unique ;

-- 單表視圖插入數(shù)據(jù): 視圖不包括所有不允許為空的字段

insert into my_v1values (null,'bc20200008','張三豐',150,'男',180,3);

-- 單表視圖插入數(shù)據(jù)

insert into my_v2values (2,'python1903','a204');

-- 多表視圖刪除數(shù)據(jù): 不能刪除數(shù)據(jù)

delete from mydb.my_v3where id=1;

-- 單表視圖刪除數(shù)據(jù)

delete from my_v2where id=4;

-- 多表視圖更新數(shù)據(jù)

update my_v3set c_id=4 where id=6;

-- 視圖: age 字段限制更新

create view my_v4as select * from my_studentwhere age>30 with check option;

-- 表示視圖的數(shù)據(jù)來源都是年齡大于30歲的,是由where age>30 決定的

-- with check option決定通過視力更新的時(shí)候,不能將已經(jīng)得到的數(shù)據(jù)age>30的改成<30的

-- 將視圖可以查看到的數(shù)據(jù)改成年齡小于30

update my_v4set age=29 where id=5;

-- 可以修改數(shù)據(jù)讓視圖可以查到: 可以改,但是沒有效果

update my_v4set age=32 where id=3

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容