MySQL left join 與 join 有所不同。 MySQL LEFT JOIN 會(huì)讀取左邊數(shù)據(jù)表的全部數(shù)據(jù),即便右邊表無(wú)對(duì)應(yīng)數(shù)據(jù)。
嘗試以下實(shí)例,理解MySQL LEFT JOIN的應(yīng)用:
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)
以上實(shí)例中使用了LEFT JOIN,該語(yǔ)句會(huì)讀取左邊的數(shù)據(jù)表tutorials_tbl的所有選取的字段數(shù)據(jù),即便在右側(cè)表tcount_tbl中沒(méi)有對(duì)應(yīng)的tutorial_author字段值。
![]() | ![]() .. 定價(jià):¥45 優(yōu)惠價(jià):¥42 更多書(shū)籍 |
![]() | ![]() .. 定價(jià):¥225 優(yōu)惠價(jià):¥213 更多書(shū)籍 |