Mysql中Join的使用實(shí)例詳解
在前幾章節(jié)中,我們已經(jīng)學(xué)會(huì)了如果在一張表中讀取數(shù)據(jù),這是相對(duì)簡(jiǎn)單的,但是在真正的應(yīng)用中經(jīng)常需要從多個(gè)數(shù)據(jù)表中讀取數(shù)據(jù)。
本章節(jié)我們將向大家介紹如何使用MySQL 的 JOIN 在兩個(gè)或多個(gè)表中查詢數(shù)據(jù)。
你可以在SELECT, UPDATE 和 DELETE 語(yǔ)句中使用Mysql 的 join 來聯(lián)合多表查詢。
以下我們將演示MySQL LEFT JOIN 和 JOIN 的使用的不同之處。
在命令提示符中使用JOIN
我們?cè)赗UNOOB數(shù)據(jù)庫(kù)中有兩張表 tcount_tbl 和 runoob_tbl。兩張數(shù)據(jù)表數(shù)據(jù)如下:
實(shí)例
嘗試以下實(shí)例:
root@host# mysql -u root -p password; Enter password:******* mysql> use RUNOOB; Database changed mysql> SELECT * FROM tcount_tbl; +-----------------+----------------+ | runoob_author | runoob_count | +-----------------+----------------+ | mahran | 20 | | mahnaz | NULL | | Jen | NULL | | Gill | 20 | | John Poul | 1 | | Sanjay | 1 | +-----------------+----------------+ 6 rows in set (0.01 sec) mysql> SELECT * from runoob_tbl; +-------------+----------------+-----------------+-----------------+ | runoob_id | runoob_title | runoob_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來連接以上兩張表來讀取runoob_tbl表中所有runoob_author字段在tcount_tbl表對(duì)應(yīng)的runoob_count字段值:
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count -> FROM runoob_tbl a, tcount_tbl b -> WHERE a.runoob_author = b.runoob_author; +-------------+-----------------+----------------+ | runoob_id | runoob_author | runoob_count | +-------------+-----------------+----------------+ | 1 | John Poul | 1 | | 3 | Sanjay | 1 | +-------------+-----------------+----------------+ 2 rows in set (0.01 sec) mysql>
在PHP腳本中使用JOIN
PHP 中使用mysql_query()函數(shù)來執(zhí)行SQL語(yǔ)句,你可以使用以上的相同的SQL語(yǔ)句作為mysql_query()函數(shù)的參數(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.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a, tcount_tbl b
WHERE a.runoob_author = b.runoob_author';
mysql_select_db('RUNOOB');
$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['runoob_author']} <br> ".
"Count: {$row['runoob_count']} <br> ".
"Tutorial ID: {$row['runoob_id']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
MySQL LEFT JOIN
MySQL left join 與 join 有所不同。 MySQL LEFT JOIN 會(huì)讀取左邊數(shù)據(jù)表的全部數(shù)據(jù),即便右邊表無對(duì)應(yīng)數(shù)據(jù)。
實(shí)例
嘗試以下實(shí)例,理解MySQL LEFT JOIN的應(yīng)用:
root@host# mysql -u root -p password; Enter password:******* mysql> use RUNOOB; Database changed mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count -> FROM runoob_tbl a LEFT JOIN tcount_tbl b -> ON a.runoob_author = b.runoob_author; +-------------+-----------------+----------------+ | runoob_id | runoob_author | runoob_count | +-------------+-----------------+----------------+ | 1 | John Poul | 1 | | 2 | Abdul S | NULL | | 3 | Sanjay | 1 | +-------------+-----------------+----------------+ 3 rows in set (0.02 sec)
以上實(shí)例中使用了LEFT JOIN,該語(yǔ)句會(huì)讀取左邊的數(shù)據(jù)表runoob_tbl的所有選取的字段數(shù)據(jù),即便在右側(cè)表tcount_tbl中沒有對(duì)應(yīng)的runoob_author字段值。
以上所述是小編給大家介紹的Mysql中Join的使用實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)本站網(wǎng)站的支持!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信