搜尋
首頁資料庫mysql教程MySQL如何實作多表查詢? MySQL多表查詢的語句

這篇文章帶給大家的內容是介紹MySQL如何實現多表查詢? MySQL多表格查詢的語句。有一定的參考價值,有需要的朋友可以參考一下,希望對你們有幫助。

建立表格

# 创建表
create table department(id int,name varchar(20));
create table employee1(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);
# 插入数据
insert into department values(200,'技术'),(201,'人力资源'),(202,'销售'),(203,'运营');

insert into employee1(name,sex,age,dep_id) values('egon','male',18,200),('alex','female',48,201),('tom','male',38,201),('yuanhao','female',28,202),('lidawei','male',18,200),('jinkezhou','female',18,204);

# 查看表
mysql> select * from employee1;
+----+-----------+--------+------+--------+
| id | name      | sex    | age  | dep_id |
+----+-----------+--------+------+--------+
|  1 | egon      | male   |   18 |    200 |
|  2 | alex      | female |   48 |    201 |
|  3 | tom       | male   |   38 |    201 |
|  4 | yuanhao   | female |   28 |    202 |
|  5 | lidawei   | male   |   18 |    200 |
|  6 | jinkezhou | female |   18 |    204 |
+----+-----------+--------+------+--------+
6 rows in set (0.00 sec)
mysql> select * from department;
+------+--------------+
| id   | name         |
+------+--------------+
|  200 | 技术       |
|  201 | 人力资源   |
|  202 | 销售       |
|  203 | 运营       |
+------+--------------+
4 rows in set (0.00 sec)

多重表格連接查詢

交叉連接

交叉連接:不適用任何符合條件。產生笛卡爾積

mysql> select * from employee1 ,department;

內連接

#內連接:找兩張表格共有的部分,相當於利用條件從笛卡爾積結果中篩選出了正確的結果。 (只連接匹配的行)

# 找两张表共有的部分,相当于利用条件从笛卡尔积结果中筛选出了正确的结果
#department没有204这个部门,因而employee表中关于204这条员工信息没有匹配出来
mysql> select * from employee1,department where employee1.dep_id=department.id;

#上面用where表示的可以用下面的内连接表示,建议使用下面的那种方法
mysql> select * from employee1 inner join department on employee1.dep_id=department.id;

# 也可以这样表示哈
mysql> select employee1.id,employee1.name,employee1.age,employee1.sex,department.name from employee1,department where employee1.dep_id=department.id;

左連接left

#優先顯示左表全部記錄。

#左链接:在按照on的条件取到两张表共同部分的基础上,保留左表的记录
mysql> select * from employee1 left join department on department.id=employee1.dep_id;

mysql> select * from department left join  employee1 on department.id=employee1.dep_id;

右連接right

優先顯示右表全部記錄。

#右链接:在按照on的条件取到两张表共同部分的基础上,保留右表的记录
mysql> select * from employee1 right join department on department.id=employee1.dep_id;
mysql> select * from department right join employee1 on department.id=employee1.dep_id;

全部連接join

mysql> select * from department full join employee1;

符合條件多表查詢

範例1:以內連接的方式查詢employee和department表,且employee表中的age欄位值必須大於25,
即找出公司所有部門中年齡大於25歲的員工

mysql> select * from employee1 inner join department on employee1.dep_id=department.id and age>25;

範例2:以內連接的方式查詢employee和department表,並且以age欄位的升序方式顯示

mysql> select * from employee1 inner join department on employee1.dep_id=department.id and age>25 and age>25 order by age asc;

子查詢

#1:子查询是将一个查询语句嵌套在另一个查询语句中。
#2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。
#3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
#4:还可以包含比较运算符:= 、 !=、> 、<p> 範例:</p><pre class="brush:php;toolbar:false"># 查询平均年龄在25岁以上的部门名
mysql> select name from department where id in ( select dep_id from employee1 group by dep_id having avg(age) > 25 );

# 查看技术部员工姓名
mysql> select name from employee1 where dep_id = (select id from department where name='技术');

# 查看小于2人的部门名
mysql> select name from department where id in (select dep_id from employee1 group by dep_id having count(id)  select * from department where id not in (select distinct dep_id from employee1);

以上是MySQL如何實作多表查詢? MySQL多表查詢的語句的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:博客园。如有侵權,請聯絡admin@php.cn刪除
mysql:blob和其他無-SQL存儲,有什麼區別?mysql:blob和其他無-SQL存儲,有什麼區別?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

mySQL添加用戶:語法,選項和安全性最佳實踐mySQL添加用戶:語法,選項和安全性最佳實踐May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串數據類型常見錯誤?MySQL:如何避免字符串數據類型常見錯誤?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

mySQL:字符串數據類型和枚舉?mySQL:字符串數據類型和枚舉?May 13, 2025 am 12:05 AM

mysqloffersechar,varchar,text,and denumforstringdata.usecharforfixed Lengttrings,varcharerforvariable長度,文本forlarger文本,andenumforenforcingDataAntegrityWithaEtofValues。

mysql blob:如何優化斑點請求mysql blob:如何優化斑點請求May 13, 2025 am 12:03 AM

優化MySQLBLOB請求可以通過以下策略:1.減少BLOB查詢頻率,使用獨立請求或延遲加載;2.選擇合適的BLOB類型(如TINYBLOB);3.將BLOB數據分離到單獨表中;4.在應用層壓縮BLOB數據;5.對BLOB元數據建立索引。這些方法結合實際應用中的監控、緩存和數據分片,可以有效提升性能。

將用戶添加到MySQL:完整的教程將用戶添加到MySQL:完整的教程May 12, 2025 am 12:14 AM

掌握添加MySQL用戶的方法對於數據庫管理員和開發者至關重要,因為它確保數據庫的安全性和訪問控制。 1)使用CREATEUSER命令創建新用戶,2)通過GRANT命令分配權限,3)使用FLUSHPRIVILEGES確保權限生效,4)定期審計和清理用戶賬戶以維護性能和安全。

掌握mySQL字符串數據類型:varchar vs.文本與char掌握mySQL字符串數據類型:varchar vs.文本與charMay 12, 2025 am 12:12 AM

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

MySQL:字符串數據類型和索引:最佳實踐MySQL:字符串數據類型和索引:最佳實踐May 12, 2025 am 12:11 AM

在MySQL中處理字符串數據類型和索引的最佳實踐包括:1)選擇合適的字符串類型,如CHAR用於固定長度,VARCHAR用於可變長度,TEXT用於大文本;2)謹慎索引,避免過度索引,針對常用查詢創建索引;3)使用前綴索引和全文索引優化長字符串搜索;4)定期監控和優化索引,保持索引小巧高效。通過這些方法,可以在讀取和寫入性能之間取得平衡,提升數據庫效率。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器