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

聯(lián)合查詢

????????基本語法:

????????????????????????select 語句1

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

????????????????????????select 語句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一定只有一個值(一行一列)

列子查詢

????????=any等價于in; -- 其中一個即可

????????any等價于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;-- 不符合要求(每個班取第一個再排序)

-- 插入學(xué)生

insert into my_student values

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

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

select * from (select * from my_student order by

height desc limit 9999999) as student group by c_id;-- 每個班選出第一個學(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)容合作請聯(lián)系作者
  • 序言:七十年代末决瞳,一起剝皮案震驚了整個濱河市货徙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌皮胡,老刑警劉巖痴颊,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異屡贺,居然都是意外死亡蠢棱,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門甩栈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來泻仙,“玉大人,你說我怎么就攤上這事量没∮褡” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵殴蹄,是天一觀的道長究抓。 經(jīng)常有香客問我,道長袭灯,這世上最難降的妖魔是什么刺下? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮稽荧,結(jié)果婚禮上怠李,老公的妹妹穿的比我還像新娘。我一直安慰自己蛤克,他們只是感情好捺癞,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著构挤,像睡著了一般髓介。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上筋现,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天唐础,我揣著相機(jī)與錄音箱歧,去河邊找鬼。 笑死一膨,一個胖子當(dāng)著我的面吹牛呀邢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播豹绪,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼价淌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瞒津?” 一聲冷哼從身側(cè)響起蝉衣,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎巷蚪,沒想到半個月后病毡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡屁柏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年啦膜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淌喻。...
    茶點(diǎn)故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡僧家,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出似嗤,到底是詐尸還是另有隱情啸臀,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布烁落,位于F島的核電站乘粒,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏伤塌。R本人自食惡果不足惜灯萍,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望每聪。 院中可真熱鬧旦棉,春花似錦、人聲如沸药薯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽童本。三九已至真屯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間穷娱,已是汗流浹背绑蔫。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工运沦, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人配深。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓携添,卻偏偏與公主長得像,于是被迫代替她去往敵國和親篓叶。 傳聞我的和親對象是個殘疾皇子烈掠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容