MySQL查詢(進階)(每個標點都是重點)

來源:互聯網
上載者:User

標籤:必須掌握   group   條件   inner   mysql   理解   nio   ***   串連   

MySQL 是工作中很普遍的需要用到的,所以必須掌握,而 之前我們一直說的都是怎麼存.

你只會存不會取有個屁用.所以希望大家在如何查詢讀取資料這方面多下點功夫.

這篇和上一篇都是乾貨,我也是第一次學.反正我就是這樣理解,這樣一遍一遍寫,一遍一遍看的才慢慢有點感覺的.

目前還整理的很亂,先將就看,等以後想起來了我會再進一步整理.

  簡單查詢 : 單表查詢和多表查詢
distinct : 去重

單表的唯一查詢用 distinct

group by : 分組

多表的唯一查詢用 group by

select * from depariment,employee;

這個就叫做笛卡爾積.

把所有記錄全都拼接一遍

查詢兩張表.把兩張表拼接到一張表,依次排開.

兩張表的所有記錄都拼接了一次(若不理解,可以實際操作看一下).

這種方式無意義.

select * from employee,department where employee.dep_id = department.id; 乾的是連表的活.

上面這是提取倆個表相同的部分.但是 but 不同的部分沒留下對不對

下面就要用的很神奇的東西

******inner join 內串連:按照 on 的條件,只取兩張表相同的部分,連結成一張虛擬表.

select * from employee inner join department onemployee.dep_id = department.id;

翻譯 : 我先查 employee, 在此基礎上繼續查 department,  on後面是條件.

select * from employee left join department onemployee.dep_id = department.id;

上面就是左連結

翻譯 : 在按照 on 條件,取到兩張表共同的基礎上,保留左表的記錄.

select * from employee rightjoin department onemployee.dep_id = department.id;

翻譯 : 保留右表

上面就是右連結下面說的是,要把左右表各自不一樣的全拿出來,稱為 : 全連結union

select * from employee left join department onemployee.dep_id = department.id

union

select * from employee rightjoin department onemployee.dep_id = department.id;

子查詢:

in 是因為可能有多個

查詢人數只有1人的部門名:

select name from department where id in ( select dep_id from employee group by dep_id having count(id) = 1 );

MySQL查詢(進階)(每個標點都是重點)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.