Articulu Directory
basa di dati MySQLl'usu internu di multi-tavula? left left join efficienza di usu
Mysql usu di cunnessione
In i capituli precedenti, avemu amparatu à leghje e dati da una tavula, chì hè relativamente simplice, ma in l'applicazioni reali hè spessu necessariu di leghje e dati da parechje tabelle di dati.
In questu capitulu vi mustraremu cumu utilizà MySQL's JOIN per dumandà dati in duie o più tavule.
Pudete aduprà Mysql's JOIN in dichjarazioni SELECT, UPDATE è DELETE per unisce dumande multi-tavule.
JOIN hè apprussimatamente divisu in e trè categorie seguenti secondu a so funzione:
- INNER JOIN (unione interna, o equijoin): Ottene registri cù campi currispondenti in dui tavule.
- LEFT JOIN (unisce à manca):Ottene tutti i registri in a tavula di manca, ancu s'ellu ùn ci hè micca un registru currispundente in u tavulu ghjustu.
- RIGHT JOIN (unisce à destra): A cuntrariu di LEFT JOIN, hè utilizatu per uttene tutti i registri in a tavula dritta, ancu s'ellu ùn ci hè micca un registru currispundente in u tavulu di manca.
Utilizendu INNER JOIN in Command Prompt
Avemu duie tavule tcount_tbl è chenweiliang_tbl in a basa di dati di chenweiliang.I dati di e duie tabelle di dati sò i seguenti:
Istanza
Pruvate i seguenti esempi:
dati di istanza di prova
mysql> use chenweiliang; Database changed mysql> SELECT * FROM tcount_tbl; +---------------+--------------+ | chenweiliang_author | chenweiliang_count | +---------------+--------------+ | 陈沩亮博客 | 10 | | chenweiliang.com | 20 | | Google | 22 | +---------------+--------------+ 3 rows in set (0.01 sec) mysql> SELECT * from chenweiliang_tbl; +-----------+---------------+---------------+-----------------+ | chenweiliang_id | chenweiliang_title | chenweiliang_author | submission_date | +-----------+---------------+---------------+-----------------+ | 1 | 学习 PHP | 陈沩亮博客 | 2017-04-12 | | 2 | 学习 MySQL | 陈沩亮博客 | 2017-04-12 | | 3 | 学习 Java | chenweiliang.com | 2015-05-01 | | 4 | 学习 Python | chenweiliang.com | 2016-03-06 | | 5 | 学习 C | FK | 2017-04-05 | +-----------+---------------+---------------+-----------------+ 5 rows in set (0.01 sec)
Dopu avemu aduprà MySQLINNER JOIN (pudete ancu omette INNER è aduprà JOIN, l'effettu hè u listessu)Per cunnette e duie tavule sopra à leghje u valore di u campu di chenweiliang_count currispundenti à tutti i campi di chenweiliang_author in a tavola di chenweiliang_tbl in a tavola tcount_tbl:
INGERICU JOIN
mysql> SELECT a.chenweiliang_id, a.chenweiliang_author, b.chenweiliang_count FROM chenweiliang_tbl a INNER JOIN tcount_tbl b ON a.chenweiliang_author = b.chenweiliang_author; +-------------+-----------------+----------------+ | a.chenweiliang_id | a.chenweiliang_author | b.chenweiliang_count | +-------------+-----------------+----------------+ | 1 | 陈沩亮博客 | 10 | | 2 | 陈沩亮博客 | 10 | | 3 | chenweiliang.com | 20 | | 4 | chenweiliang.com | 20 | +-------------+-----------------+----------------+ 4 rows in set (0.00 sec)
A dichjarazione SQL sopra hè equivalente à:
WHERE clause
mysql> SELECT a.chenweiliang_id, a.chenweiliang_author, b.chenweiliang_count FROM chenweiliang_tbl a, tcount_tbl b WHERE a.chenweiliang_author = b.chenweiliang_author; +-------------+-----------------+----------------+ | a.chenweiliang_id | a.chenweiliang_author | b.chenweiliang_count | +-------------+-----------------+----------------+ | 1 | 陈沩亮博客 | 10 | | 2 | 陈沩亮博客 | 10 | | 3 | chenweiliang.com | 20 | | 4 | chenweiliang.com | 20 | +-------------+-----------------+----------------+ 4 rows in set (0.01 sec)
MySQL LEFT JOIN
MySQL left join hè diversu da join. MySQL LEFT JOIN leghjerà tutte e dati in a tabella di dati di manca, ancu s'è a tavula di dritta ùn hà micca dati currispondenti.
Istanza
Pruvate i seguenti esempi per chenweiliang_tbl per a tavola di manca,tcount_tbl Per a tavola ghjusta, capisce l'applicazione di MySQL LEFT JOIN:
LEFT JOIN
mysql> SELECT a.chenweiliang_id, a.chenweiliang_author, b.chenweiliang_count FROM chenweiliang_tbl a LEFT JOIN tcount_tbl b ON a.chenweiliang_author = b.chenweiliang_author; +-------------+-----------------+----------------+ | a.chenweiliang_id | a.chenweiliang_author | b.chenweiliang_count | +-------------+-----------------+----------------+ | 1 | 陈沩亮博客 | 10 | | 2 | 陈沩亮博客 | 10 | | 3 | chenweiliang.com | 20 | | 4 | chenweiliang.com | 20 | | 5 | FK | NULL | +-------------+-----------------+----------------+ 5 rows in set (0.01 sec)
In l'esempiu di sopra, LEFT JOIN hè utilizatu. Questa dichjarazione hà da leghje tutti i dati di u campu selezziunatu di a tabella di dati chenweiliang_tbl à a manca, ancu s'ellu ùn ci hè micca un valore di campu currispundente di chenweiliang_author in a tavola tcount_tbl à a diritta.
MySQL RIGHT JOIN
MySQL RIGHT JOIN leghjerà tutte e dati in a tavula di dati à a diritta, ancu s'ellu ùn ci hè micca dati currispundenti in a tavola à manca.
Istanza
Pruvate i seguenti esempi per chenweiliang_tbl per a tavola di manca,tcount_tbl Per a tavola ghjusta, capisce l'applicazione di MySQL RIGHT JOIN:
RIGHT JOIN
mysql> SELECT a.chenweiliang_id, a.chenweiliang_author, b.chenweiliang_count FROM chenweiliang_tbl a RIGHT JOIN tcount_tbl b ON a.chenweiliang_author = b.chenweiliang_author; +-------------+-----------------+----------------+ | a.chenweiliang_id | a.chenweiliang_author | b.chenweiliang_count | +-------------+-----------------+----------------+ | 1 | 陈沩亮博客 | 10 | | 2 | 陈沩亮博客 | 10 | | 3 | chenweiliang.com | 20 | | 4 | chenweiliang.com | 20 | | NULL | NULL | 22 | +-------------+-----------------+----------------+ 5 rows in set (0.01 sec)
RIGHT JOIN hè utilizatu in l'esempiu di sopra, sta dichjarazione leghjerà tutti i dati di u campu selezziunatu di a tavola di dati dritta tcount_tbl, ancu s'ellu ùn ci hè micca un valore di u campu currispundente chenweiliang_author in a tavola sinistra chenweiliang_tbl.
Utilizendu JOIN in script PHP
A funzione mysqli_query () hè aduprata in PHP per eseguisce dichjarazioni SQL, pudete aduprà a stessa dichjarazione SQL cum'è sopra cum'è un paràmetru à a funzione mysqli_query ().
Pruvate u seguente esempiu:
MySQL ORDER BY test:
<? php $dbhost = 'localhost:3306'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } // 设置编码,防止中文乱码 mysqli_query($conn , "set names utf8"); $sql = 'SELECT a.chenweiliang_id, a.chenweiliang_author, b.chenweiliang_count FROM chenweiliang_tbl a INNER JOIN tcount_tbl b ON a.chenweiliang_author = b.chenweiliang_author'; mysqli_select_db( $conn, 'chenweiliang' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('无法读取数据: ' . mysqli_error($conn)); } echo '<h2>陈沩亮博客 MySQL JOIN 测试<h2>'; echo '<table border="1"><tr><td>教程 ID</td><td>作者</td><td>登陆次数</td></tr>'; while($row = mysqli_fetch_array($retval, MYSQL_ASSOC)) { echo "<tr><td> {$row['chenweiliang_id']}</td> ". "<td>{$row['chenweiliang_author']} </td> ". "<td>{$row['chenweiliang_count']} </td> ". "</tr>"; } echo '</table>'; mysqli_close($conn); ?>
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) hà spartutu "Utilizazione multi-tavula di a basa di dati MySQL inner join? left Left Join Usage Efficiency", vi aiuterà.
Benvenuti à sparte u ligame di stu articulu:https://www.chenweiliang.com/cwl-488.html
Benvenuti à u canali Telegram di u blog di Chen Weiliang per avè l'ultime aghjurnamenti!
📚 Questa guida cuntene un valore enormu, 🌟Questa hè una rara opportunità, ùn mancate micca! ⏰⌛💨
Condividi è mi piace se ti piace!
A vostra spartera è Mi piace sò a nostra motivazione cuntinua!