W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
在前幾章節(jié)中,我們已經(jīng)學會了如果在一張表中讀取數(shù)據(jù),這是相對簡單的,但是在真正的應用中經(jīng)常需要從多個數(shù)據(jù)表中讀取數(shù)據(jù)。
本章節(jié)我們將向大家介紹如何使用MySQL 的 JOIN 在兩個或多個表中查詢數(shù)據(jù)。
你可以在SELECT, UPDATE 和 DELETE 語句中使用Mysql 的 join 來聯(lián)合多表查詢。
以下我們將演示MySQL LEFT JOIN 和 JOIN 的使用的不同之處。
我們在TUTORIALS數(shù)據(jù)庫中有兩張表 tcount_tbl 和 tutorials_tbl。兩張數(shù)據(jù)表數(shù)據(jù)如下:
嘗試以下實例:
root@host# mysql -u root -p password; Enter password:******* mysql> use TUTORIALS; Database changed mysql> SELECT * FROM tcount_tbl; +-----------------+----------------+ | tutorial_author | tutorial_count | +-----------------+----------------+ | mahran | 20 | | mahnaz | NULL | | Jen | NULL | | Gill | 20 | | John Poul | 1 | | Sanjay | 1 | +-----------------+----------------+ 6 rows in set (0.01 sec) mysql> SELECT * from tutorials_tbl; +-------------+----------------+-----------------+-----------------+ | tutorial_id | tutorial_title | tutorial_author | submission_date | +-------------+----------------+-----------------+-----------------+ | 1 | Learn PHP | John Poul | 2007-05-24 | | 2 | Learn MySQL | Abdul S | 2007-05-24 | | 3 | JAVA Tutorial | Sanjay | 2007-05-06 | +-------------+----------------+-----------------+-----------------+ 3 rows in set (0.00 sec) mysql>
接下來我們就使用MySQL的JOIN來連接以上兩張表來讀取tutorials_tbl表中所有tutorial_author字段在tcount_tbl表對應的tutorial_count字段值:
mysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count -> FROM tutorials_tbl a, tcount_tbl b -> WHERE a.tutorial_author = b.tutorial_author; +-------------+-----------------+----------------+ | tutorial_id | tutorial_author | tutorial_count | +-------------+-----------------+----------------+ | 1 | John Poul | 1 | | 3 | Sanjay | 1 | +-------------+-----------------+----------------+ 2 rows in set (0.01 sec) mysql>
PHP 中使用mysql_query()函數(shù)來執(zhí)行SQL語句,你可以使用以上的相同的SQL語句作為mysql_query()函數(shù)的參數(shù)。
嘗試如下實例:
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count FROM tutorials_tbl a, tcount_tbl b WHERE a.tutorial_author = b.tutorial_author'; mysql_select_db('TUTORIALS'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Author:{$row['tutorial_author']} <br> ". "Count: {$row['tutorial_count']} <br> ". "Tutorial ID: {$row['tutorial_id']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully\n"; mysql_close($conn); ?>
MySQL left join 與 join 有所不同。 MySQL LEFT JOIN 會讀取左邊數(shù)據(jù)表的全部數(shù)據(jù),即便右邊表無對應數(shù)據(jù)。
嘗試以下實例,理解MySQL LEFT JOIN的應用:
root@host# mysql -u root -p password; Enter password:******* mysql> use TUTORIALS; Database changed mysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count -> FROM tutorials_tbl a LEFT JOIN tcount_tbl b -> ON a.tutorial_author = b.tutorial_author; +-------------+-----------------+----------------+ | tutorial_id | tutorial_author | tutorial_count | +-------------+-----------------+----------------+ | 1 | John Poul | 1 | | 2 | Abdul S | NULL | | 3 | Sanjay | 1 | +-------------+-----------------+----------------+ 3 rows in set (0.02 sec)
以上實例中使用了LEFT JOIN,該語句會讀取左邊的數(shù)據(jù)表tutorials_tbl的所有選取的字段數(shù)據(jù),即便在右側表tcount_tbl中沒有對應的tutorial_author字段值。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: