Home  >  Article  >  Database  >  Detailed examples of the application of Join in Mysql

Detailed examples of the application of Join in Mysql

怪我咯
怪我咯Original
2017-04-30 10:18:451247browse

In this chapter we will introduce to you how to use MySQL's JOIN to query data in two or more tables. Friends who are interested in join in mysql should learn together

In the previous chapters, we have learned how to read data in a table. This is relatively simple, but it is often needed in real applications. Read data from multiple data tables.

In this chapter we will introduce to you how to use MySQL's JOIN to query data in two or more tables.

You can use Mysql's join in SELECT, UPDATE and DELETE statements to combine multiple table queries.

Below we will demonstrate the difference between the use of MySQL LEFT JOIN and JOIN.

Using JOIN in the command prompt

We have two tables tcount_tbl and runoob_tbl in the RUNOOB database. The data of the two data tables are as follows:

Example

Try the following example:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use RUNOOB;
Database changed
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| runoob_author | runoob_count |
+-----------------+----------------+
| mahran   |    20 |
| mahnaz   |   NULL |
| Jen    |   NULL |
| Gill   |    20 |
| John Poul  |    1 |
| Sanjay   |    1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
mysql> SELECT * from runoob_tbl;
+-------------+----------------+-----------------+-----------------+
| runoob_id | runoob_title | runoob_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>

Next we will use MySQL's JOIN to connect the above two tables to read the runoob_tbl table The runoob_count field value corresponding to all runoob_author fields in the tcount_tbl table:

mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count
 -> FROM runoob_tbl a, tcount_tbl b
 -> WHERE a.runoob_author = b.runoob_author;
+-------------+-----------------+----------------+
| runoob_id | runoob_author | runoob_count |
+-------------+-----------------+----------------+
|   1 | John Poul  |    1 |
|   3 | Sanjay   |    1 |
+-------------+-----------------+----------------+
2 rows in set (0.01 sec)
mysql>

Use JOIN in PHP script

Use in PHP mysql_query()functionTo execute a SQL statement, you can use the same SQL statement above as the parameter of the mysql_query()function.

Try the following example:

<?php
$dbhost = &#39;localhost:3036&#39;;
$dbuser = &#39;root&#39;;
$dbpass = &#39;rootpassword&#39;;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
 die(&#39;Could not connect: &#39; . mysql_error());
}
$sql = &#39;SELECT a.runoob_id, a.runoob_author, b.runoob_count
  FROM runoob_tbl a, tcount_tbl b
  WHERE a.runoob_author = b.runoob_author&#39;;
mysql_select_db(&#39;RUNOOB&#39;);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
 die(&#39;Could not get data: &#39; . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
 echo "Author:{$row[&#39;runoob_author&#39;]} <br> ".
   "Count: {$row[&#39;runoob_count&#39;]} <br> ".
   "Tutorial ID: {$row[&#39;runoob_id&#39;]} <br> ".
   "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

MySQL LEFT JOIN

MySQL left join is different from join. MySQL LEFT JOIN will read all the data in the left data table, even if there is no corresponding data in the right table.

Example

Try the following example to understand the application of MySQL LEFT JOIN:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use RUNOOB;
Database changed
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count
 -> FROM runoob_tbl a LEFT JOIN tcount_tbl b
 -> ON a.runoob_author = b.runoob_author;
+-------------+-----------------+----------------+
| runoob_id | runoob_author | runoob_count |
+-------------+-----------------+----------------+
|   1 | John Poul  |    1 |
|   2 | Abdul S   |   NULL |
|   3 | Sanjay   |    1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)

LEFT JOIN is used in the above example. This statement will read the data table runoob_tbl on the left All selected field data, even if there is no corresponding runoob_author field value in the right table tcount_tbl.


The above is the detailed content of Detailed examples of the application of Join in Mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn