In the previous chapters, we have learned how to read data from one table, which is relatively simple, but in real applications, we often need to read data from multiple tables. In this section, we will introduce how to use MySQL JOIN to query data in two or more tables. You can use MySQL join to combine multiple tables in SELECT, UPDATE and DELETE statements. Below we will demonstrate the difference between the use of MySQL LEFT JOIN and JOIN. Using JOIN in Command Prompt We have two tables tcount_tbl and runoob_tbl in the RUNOOB database. The data in the two data tables are as follows: Examples Try the following examples: 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 use MySQL JOIN to connect the above two tables to read the runoob_count field values corresponding to all runoob_author fields in the runoob_tbl table 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> Using JOIN in PHP scripts PHP uses the mysql_query() function to execute SQL statements. You can use the same SQL statement 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 table, even if there is no corresponding data in the right table. Examples Try the following examples 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) In the above example, LEFT JOIN is used. This statement will read all selected field data from the left data table runoob_tbl, even if there is no corresponding runoob_author field value in the right table tcount_tbl. The above is a detailed explanation of the use of Join in MySQL introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: VUE implements bottom suction button
>>: How to configure Java environment variables in Linux system
Table of contents 0x01. Install the Pagoda Panel ...
Table of contents Install Tomcat with Docker Use ...
1|0 Compile the kernel (1) Run the uname -r comma...
<meta http-equiv="X-UA-Compatible" c...
Introduction to Nginx Nginx is a high-performance...
In web design, it is very important to use an org...
This article example shares the specific code of ...
Preface When we deploy applications to servers as...
animation Define an animation: /*Set a keyframe t...
1. Function : Allows the parent component to inse...
Four practical vue custom instructions 1. v-drag ...
We all know that the commonly used positioning me...
Table of contents Example 1 Example 2 Example 3 E...
Table of contents forEach() (ES6) method map() (E...
Table of contents 1. Why NanoID is replacing UUID...