Mysql-using-joins
提供:Dev Guides
MySQl結合の使用
前の章では、一度に1つのテーブルからデータを取得していました。 これは単純なテイクには十分ですが、実際のMySQLの使用のほとんどでは、多くの場合、1つのクエリで複数のテーブルからデータを取得する必要があります。
単一のSQLクエリで複数のテーブルを使用できます。 MySQLに参加するという行為は、2つ以上のテーブルを1つのテーブルに粉砕することを指します。
SELECT、UPDATE、およびDELETEステートメントでJOINSを使用して、MySQLテーブルを結合できます。 単純なMySQL JOINとは異なるLEFT JOINの例もあります。
コマンドプロンプトでの結合の使用
チュートリアルに2つのテーブル 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>
これで、これら2つのテーブルを結合する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スクリプトでの結合の使用
PHPスクリプトでは、上記の任意のSQLクエリを使用できます。 SQLクエリをPHP関数* mysql_query()*に渡すだけで、通常の方法で結果を取得できます。
例
次の例-
<?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 LEFT JOIN
MySQLの左結合は、単純な結合とは異なります。 MySQL LEFT JOINは、左側のテーブルをさらに考慮します。
*LEFT JOIN* を実行すると、同じ方法で一致するすべてのレコードが取得され、IN ADDITIONで結合の左側のテーブルにある一致しない各レコードに対して追加のレコードが取得されます。言及を取得します。
例
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に慣れるには、さらに練習する必要があります。 これは、MySQL/SQLの少し複雑な概念であり、実際の例を実行するうちに、より明確になるでしょう。