mysql的join语句使用_在MySQL中使用JOIN语句进行连接操作的详细教程
到目前,我們已經(jīng)學(xué)習了從一個表中獲取數(shù)據(jù)。這是簡單的需要,但在大多數(shù)現(xiàn)實MySQL的使用,經(jīng)常需要將數(shù)據(jù)從多個表中的一個單一的查詢。
可以使用多個表中的單一SQL查詢。在MySQL中聯(lián)接(join)行為是指兩個或多個表到一個表中可以使用連接在SELECT,UPDATE和DELETE語句中加入MySQL表。我們將看到一個例子LEFT JOIN簡單的MySQL連接。
在命令提示符使用聯(lián)接:
假設(shè)我們兩個表的教程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)在,加入這兩個表我們可以編寫一個SQL查詢。這個查詢會選擇所有的作者從表tutorials_tbl,從tcount_tbl會拿起相應(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左連接:
一個簡單的連接和一個MySQL左連接是不同的。一個MySQL LEFT JOIN提供了額外的考慮到在左邊的表。
如果做了LEFT JOIN,得到的所有記錄以同樣的方式相匹配,此外,得到一個額外的記錄每個不匹配的記錄,在左表中的聯(lián)接 - 從而保證了每一個作者得到關(guān)聯(lián)(本例子中):
實例:
試試下面的例子就明白了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)
需要做更多的實踐才能熟悉JOINS。這是一個復(fù)雜的概念,在MySQL/SQL將變得更加清晰。
本文標題: 在MySQL中使用JOIN語句進行連接操作的詳細教程
本文地址: http://www.cppcns.com/shujuku/mysql/125403.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的mysql的join语句使用_在MySQL中使用JOIN语句进行连接操作的详细教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis配置id自增mysql_m
- 下一篇: python3.7官网中文官网_Pyth