在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)庫(kù)/ MySQL Using Join
MySQL 復(fù)制表
MySQL ALTER 命令
MySQL 安裝
MySQL 日期與時(shí)間方面的函數(shù)
MySQL SQL Injection
MySQL 排序結(jié)果
MySQL 臨時(shí)表
MySQL 介紹
MySQL 數(shù)據(jù)導(dǎo)出
MySQL 索引
MySQL 數(shù)值函數(shù)
MySQL 更新查詢
MySQL UNION 關(guān)鍵字
MySQL RAND 函數(shù)
創(chuàng)建 MySQL 數(shù)據(jù)庫(kù)
MySQL AVG 函數(shù)
MySQL Using Join
MySQL Handling Duplicates
MySQL SUM 函數(shù)
MySQL 數(shù)據(jù)類型
MySQL 插入查詢
MySQL 字符串函數(shù)
MySQL Using Sequences
MySQL 管理
MySQL 數(shù)據(jù)導(dǎo)入
MySQL BETWEEN 子句
MySQL MIN 函數(shù)
創(chuàng)建 MySQL 表
MySQL Group By 子句
MySQL COUNT 函數(shù)
MySQL 匯報(bào)
MySQL 選擇數(shù)據(jù)庫(kù)
MySQL Where Clause
MySQL 選擇查詢
MySQL Like Clause
MySQL 正則表達(dá)式
一些非常有用的學(xué)習(xí)資源
MySQL NULL Values
MySQL 刪除查詢
MySQL 數(shù)據(jù)庫(kù)信息
一些有用的 MySQL 函數(shù)與子句
MySQL 刪除表
MySQL MAX 函數(shù)
MySQL SQRT 函數(shù)
MySQL 終止數(shù)據(jù)庫(kù)
連接 MySQL 服務(wù)器
MySQL IN 子句
MySQL CONCAT 函數(shù)
MySQL PHP語(yǔ)法

MySQL Using Join

迄今為止,我們每次只能從一張表里獲取數(shù)據(jù)。這足以應(yīng)付簡(jiǎn)單的任務(wù)了,但大多數(shù)真實(shí)的 MySQL 應(yīng)用場(chǎng)景卻經(jīng)常會(huì)需要通過(guò)一次查詢,從多張表中獲取數(shù)據(jù)。

在一個(gè) SQL 查詢中使用多張表,聯(lián)結(jié)(join)行為在 MySQL 數(shù)據(jù)庫(kù)中指的就是將2張或更多的表合為一張表。

你可以在 SELECT、UPDATE、DELETE語(yǔ)句中使用 JOIN 來(lái)聯(lián)結(jié) MySQL 表。下面還將介紹一個(gè)左聯(lián)結(jié)(LEFT JOIN)的范例,了解一下它與 JOIN 的區(qū)別。

在命令行中使用 JOIN

假設(shè)數(shù)據(jù)庫(kù) TUTORIALS 中有兩張表:tcount_tbltutorials_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>

上例通過(guò)一個(gè) SQL 查詢將兩張表聯(lián)結(jié)到一起。這次查詢選擇了表 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 腳本中使用JOIN

可以在 PHP 腳本中使用以前學(xué)到過(guò)的任何一種 SQL 查詢。只需要將 SQL 查詢傳入 PHP 函數(shù) mysql_query() 中,就能按照之前的方式獲得結(jié)果。

范例

相關(guān)范例如下:

<?php
$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']}  <br> ".
         "Count: {$row['tutorial_count']} <br> ".
         "Tutorial ID: {$row['tutorial_id']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

MySQL的左聯(lián)結(jié)(LEFT JOIN)

MySQL 的左聯(lián)結(jié)(LEFT JOIN)與簡(jiǎn)單使用 JOIN 的效果不同。左聯(lián)結(jié)側(cè)重考慮左側(cè)的表。

如果進(jìn)行左聯(lián)結(jié),除了得到所有跟以上聯(lián)結(jié)同樣的匹配記錄之外,還會(huì)得到左側(cè)表中未曾匹配的記錄,從而保證了(在該范例中)照顧到了每一位作者。

范例

下面這個(gè)范例可以幫我們更好地理解左聯(lián)結(jié)。

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)

必須多加練習(xí),才能熟悉 JOIN。這是 MySQL/SQL 中的一個(gè)比較復(fù)雜的概念,必須經(jīng)過(guò)一番真實(shí)的案例磨煉才能真正地掌握它。