Artikulu direktorioa
MySQL datu-baseainner join mahai anitzeko erabilera? ezkerrean ezkerreko batu erabilera-eraginkortasuna
Mysql konexioaren erabilera
Aurreko kapituluetan, taula bateko datuak irakurtzen ikasi dugu, eta hori nahiko erraza da, baina aplikazio errealetan askotan datu-tauletako datuak irakurtzea beharrezkoa da.
Kapitulu honetan MySQL-ren JOIN nola erabili erakutsiko dizugu bi taula edo gehiagotan datuak kontsultatzeko.
Mysql-en JOIN erabil dezakezu SELECT, UPDATE eta DELETE adierazpenetan taula anitzeko kontsultak batzeko.
JOIN, gutxi gorabehera, hiru kategoria hauetan banatzen da bere funtzioaren arabera:
- INNER JOIN (barne-juntura edo equijoin): Lortu bi tauletan bat datozen eremuak dituzten erregistroak.
- LEFT JOIN (ezkerreko batuketa):Lortu erregistro guztiak ezkerreko taulan, eskuineko taulan bat datorren erregistrorik ez badago ere.
- ESKUINEKO BATU (eskuineko juntadura): LEFT JOIN-en aurka, eskuineko taulako erregistro guztiak lortzeko erabiltzen da, nahiz eta ezkerreko taulan dagokien bat datorren erregistrorik ez egon.
INNER JOIN erabiliz komando-gonbitan
Tcount_tbl eta chenweiliang_tbl bi taula ditugu chenweiliang datu-basean.Bi datu-tauletako datuak honako hauek dira:
adibidea
Saiatu adibide hauek:
proba-instantziaren datuak
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)
Ondoren, MySQL erabiliko duguINNER JOIN (INNER ere ezabatu dezakezu eta JOIN erabil dezakezu, efektua berdina da)Goiko bi taulak lotzeko tcount_tbl taulako chenweiliang_tbl taulako chenweiliang_tbl taulako chenweiliang_author eremu guztiei dagokien chenweiliang_count eremuaren balioa irakurtzeko:
INNER 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)
Goiko SQL adierazpena honen baliokidea da:
NON klausula
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-ek EZKER ERAKUNDEA
MySQL ezkerreko batzea batutik ezberdina da. MySQL LEFT JOIN ezkerreko datuen taulako datu guztiak irakurriko ditu, nahiz eta eskuineko taulak dagokion daturik ez izan.
adibidea
Saiatu honako adibide hauek chenweiliang_tbl ezkerreko mahairako,tcount_tbl Taula egokia lortzeko, ulertu MySQL LEFT JOIN aplikazioa:
EZKERRERA BATU
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)
Goiko adibidean, LEFT JOIN erabiltzen da, eta adierazpen honek ezkerreko chenweiliang_tbl datu-taularen hautatutako eremuko datu guztiak irakurriko ditu, eskuineko tcount_tbl taulan chenweiliang_author-ren dagokion eremu-baliorik ez badago ere.
MySQL ESKUBIDEAN SARTU
MySQL RIGHT JOIN eskuineko datu-taulan dauden datu guztiak irakurriko ditu, ezkerreko taulan dagokien daturik ez badago ere.
adibidea
Saiatu honako adibide hauek chenweiliang_tbl ezkerreko mahairako,tcount_tbl Taula egokia lortzeko, ulertu MySQL RIGHT JOIN aplikazioa:
ESKUBIAN SARTU
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 erabiltzen da goiko adibidean, adierazpen honek tcount_tbl eskuineko datu-taularen aukeratutako eremuko datu guztiak irakurriko ditu, nahiz eta dagokion chenweiliang_author eremuko baliorik ez egon ezkerreko taulan chenweiliang_tbl.
JOIN erabiliz PHP gidoian
mysqli_query() funtzioa PHPn erabiltzen da SQL adierazpenak exekutatzeko, goiko SQL instrukzio bera erabil dezakezu mysqli_query() funtzioaren parametro gisa.
Saiatu hurrengo adibidea:
MySQL ORDENATU proba:
<? 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 bloga ( https://www.chenweiliang.com/ ) partekatu du "MySQL datu-basearen barruko elkarketa-taula anitzeko erabilera? ezkerrera bat egin Erabilera eraginkortasuna", lagunduko dizu.
Ongi etorri artikulu honen esteka partekatzera:https://www.chenweiliang.com/cwl-488.html
Ongi etorri Chen Weiliang-en blogeko Telegram kanalera azken eguneraketak jasotzeko!
📚 Gida honek balio handia du, 🌟Aukera arraroa da hau, ez galdu! ⏰⌛💨
Partekatu eta gustatzen bazaizu!
Zure partekatzea eta gustukoak dira gure etengabeko motibazioa!