mysql的join语句使用_在MySQL中使用JOIN语句进行连接操作的详细教程
到目前,我們已經(jīng)學(xué)習(xí)了從一個(gè)表中獲取數(shù)據(jù)。這是簡(jiǎn)單的需要,但在大多數(shù)現(xiàn)實(shí)MySQL的使用,經(jīng)常需要將數(shù)據(jù)從多個(gè)表中的一個(gè)單一的查詢。
可以使用多個(gè)表中的單一SQL查詢。在MySQL中聯(lián)接(join)行為是指兩個(gè)或多個(gè)表到一個(gè)表中可以使用連接在SELECT,UPDATE和DELETE語(yǔ)句中加入MySQL表。我們將看到一個(gè)例子LEFT JOIN簡(jiǎn)單的MySQL連接。
在命令提示符使用聯(lián)接:
假設(shè)我們兩個(gè)表的教程tcount_tbl和tutorials_tbl的完整列表如下:
例子:
試試下面的例子:
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>
現(xiàn)在,加入這兩個(gè)表我們可以編寫一個(gè)SQL查詢。這個(gè)查詢會(huì)選擇所有的作者從表tutorials_tbl,從tcount_tbl會(huì)拿起相應(yīng)數(shù)量的教程。
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腳本中使用聯(lián)接:
可以使用任何上述的SQL查詢的PHP腳本。只需要通過PHP函數(shù)mysql_query()執(zhí)行SQL查詢,然后用常規(guī)方法獲取結(jié)果。
例子:
試試下面的例子:
$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']}
".
"Count: {$row['tutorial_count']}
".
"Tutorial ID: {$row['tutorial_id']}
".
"--------------------------------
";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
MySQL左連接:
一個(gè)簡(jiǎn)單的連接和一個(gè)MySQL左連接是不同的。一個(gè)MySQL LEFT JOIN提供了額外的考慮到在左邊的表。
如果做了LEFT JOIN,得到的所有記錄以同樣的方式相匹配,此外,得到一個(gè)額外的記錄每個(gè)不匹配的記錄,在左表中的聯(lián)接 - 從而保證了每一個(gè)作者得到關(guān)聯(lián)(本例子中):
實(shí)例:
試試下面的例子就明白了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)
需要做更多的實(shí)踐才能熟悉JOINS。這是一個(gè)復(fù)雜的概念,在MySQL/SQL將變得更加清晰。
本文標(biāo)題: 在MySQL中使用JOIN語(yǔ)句進(jìn)行連接操作的詳細(xì)教程
本文地址: http://www.cppcns.com/shujuku/mysql/125403.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的mysql的join语句使用_在MySQL中使用JOIN语句进行连接操作的详细教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis配置id自增mysql_m
- 下一篇: mysql8连接数据库显示cache_M