国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL Join聯接

2024-07-24 12:32:40
字體:
來源:轉載
供稿:網友
        到目前為止,我們只是從一個表讀取數據。這是相對簡單的,但在大多數現實中的MySQL使用,需要從多個表中,在單個查詢獲得數據。
 
      可以在單個SQL查詢中使用多個表。連接MySQL中的行在兩個或多個表到一個表。
 
       可以使用Join在SELECT,UPDATE和DELETE語句加入MySQL表。我們將看到LEFT JOIN的例子, 這與簡單的MySQL JOIN有所不同。
 
1、在命令提示符中使用聯接
       假設我們有兩個表 tcount_tbl 和 tutorials_tbl,在數據庫:test ,完整列表如下:
 
示例
試試下面的例子:
 
root@host# mysql -u root -p password;
Enter password:
mysql> use test;
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>
現在,我們可以寫一個SQL查詢來連接這兩個表。此查詢將從表 tutorials_tbl 和 tcount_tbl 選擇所有作者的在線教程數量。
 
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連接
可以使用上述的任何SQL查詢在PHP腳本中。只需要傳遞SQL查詢到PHP的mysql_query()函數,然后以通常的方式獲取結果。
 
示例
試試下面的例子:
 
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$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('test');
$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 LEFT JOIN
MySQL左連接不同于簡單連接。MySQL LEFT JOIN提供該表額外字段在左側。
 
如果使用LEFT JOIN,得到的所有記錄的匹配方式相同,在左邊表中得到的每個記錄不匹配也會有一個額外的記錄。 從而確保(在本例子),每次作者信息都會列出:
 
示例
試試下面的例子就明白了 LEFT JOIN 的用法(注:第2條,作者Abdul S沒有寫在線教程也被列出來了):
 
root@host# mysql -u root -p password;
Enter password:
mysql> use test;
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)
需要做更多的練習,以熟悉連接。這個概念是有點復雜,做這樣實際例子,在MySQL/SQL將變得更加清晰。
 
 

(編輯:武林網)

上一篇:MySQL Order By排序結果

下一篇:MySQL NULL值

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 噶尔县| 武功县| 赣榆县| 玛曲县| 汶上县| 淮南市| 沁水县| 封丘县| 南昌市| 弋阳县| 武强县| 辉南县| 香河县| 田东县| 阿勒泰市| 古丈县| 望城县| 常熟市| 石楼县| 武功县| 博客| 长葛市| 阳城县| 壤塘县| 仁化县| 龙井市| 绥德县| 山东| 桑日县| 虎林市| 临清市| 讷河市| 正蓝旗| 贵溪市| 涟源市| 广平县| 天峨县| 尼木县| 南郑县| 青海省| 望都县|