⑧MySQL(聯(lián)合查詢、標(biāo)量-列子查詢、行-表子查詢、exists子查詢)

mysql.exe -h localhost -P 3306 -u root -p

use mydb;?????——???? 進(jìn)入數(shù)據(jù)庫(kù)

查看:show index from 表名\G

desc:查看表結(jié)構(gòu)

select * from 表名:查詢所有數(shù)據(jù)?


聯(lián)合查詢

????????基本語(yǔ)法:

????????????????????????select 語(yǔ)句1

????????????????????????union [union 選項(xiàng)]

????????????????????????select 語(yǔ)句2……

????????union 選項(xiàng)

????????????????????????all:保留所有,不管重復(fù)

????????????????????????distinct:去重,默認(rè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,id from my_student;

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

(select * from my_student where sex='男' order by age asc limit 9999999)

union

(select * from my_student where sex='女' order by age desc limit 9999999);


子查詢

按位置分類

????????from子查詢

????????where子查詢

????????exists子查詢

按結(jié)果分類(不用背,了解,不考)

????????標(biāo)量子查詢:一行一列

????????列子查詢:一列多行

????????行子查詢:多列一行/多行多列

????????表子查詢:多行多列

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

select * from my_student where c_id=(select id from my_class where c_name='Python1910');-- id一定只有一個(gè)值(一行一列)

列子查詢

????????=any等價(jià)于in; -- 其中一個(gè)即可

????????any等價(jià)于some; -- 二者是一樣的

????????=all為全部

-- 列子查詢(in偶爾用)

select * from my_student where c_id in(select id from my_class);

-- any,some,all? ??——? ? 肯定(不常用,了解)

select * from my_student where c_id=any(select id from my_class);

select * from my_student where c_id=some(select id from my_class);

select * from my_student where c_id=all(select id from my_class);

-- any,some,all? ??——? ? 否定(不常用,了解)

select * from my_student where c_id!=any(select id from my_class);-- 所有的結(jié)果(NULL除外)

select * from my_student where c_id!=some(select id from my_class);-- 所有的結(jié)果(NULL除外)

select * from my_student where c_id!=all(select id from my_class);--(NULL除外)

-- 查詢年齡最大且身高最高

select * from my_student where

age=(select max(age) from my_student)

and

height=(select max(height) from my_student);

-- 行子查詢

select * from my_student where

-- (age,height)稱為行元素

(age,height)=(select max(age),max(height) from

my_student);


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

-- 表子查詢

select * from my_student group by c_id order by height desc;-- 不符合要求(每個(gè)班取第一個(gè)再排序)

-- 插入學(xué)生

insert into my_student values

(null,'bc20200007','小紅','女',33,185,1);

-- 查找每個(gè)班身高最高的學(xué)生(加limit 9999999才能查出預(yù)想結(jié)果,有的不加也行,根據(jù)數(shù)據(jù)庫(kù)版本決定)

select * from (select * from my_student order by

height desc limit 9999999) as student group by c_id;-- 每個(gè)班選出第一個(gè)學(xué)生


exists子查詢

select exists(select * from my_student);

select exists(select * from my_student where id=100);

-- exists子查詢

select * from my_student where

exists(select * from? my_class);-- 是否成立

select * from my_student where

exists(select * from? my_class where id=3);

select * from my_student where

exists(select * from? my_class where id=2);

最后編輯于
?著作權(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ù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過(guò)簡(jiǎn)信或評(píng)論聯(lián)系作者。

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

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