Home >Database >Mysql Tutorial >Detailed examples of the application of Join in Mysql
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 = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a, tcount_tbl b WHERE a.runoob_author = b.runoob_author'; mysql_select_db('RUNOOB'); $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['runoob_author']} <br> ". "Count: {$row['runoob_count']} <br> ". "Tutorial ID: {$row['runoob_id']} <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!