How to use table join in MySQL and PHP
This article is about how to use table joins in MySQL and PHP. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Table join
A table join (JOIN) is a join condition between multiple tables. Correlate tables so that data can be obtained from multiple tables.
Table join syntax:
SELECT table name 1. Column name, table name 2. Column name FROM Table 1, Table 2 WHERE condition
Several ways to join tables:
Internal connection: join or inner join
Self-join: connections in the same table
External connection:
Left outside connection, left join
Right outer connection, right join
Full external connection, full join
Data (table1)
Data (table2)
Internal connection
An inner join deletes all rows from the result table that do not match the other joined tables. (intersection)
Format:
SELECT A.c1, B.c2 FROM A JOIN B ON A.c3 = B.c3
Example:
Output result:
Database link successful
The SQL statement was executed successfully!
Array
(
[name] = > Yang Fugui
[age] = > 18
[class] = > math class
)
Array
(
[name] = > Yang Mei
[age] = > 19
[class] = > Chinese course
)
Array
(
[name] = > Yang Jianguo
[age] = > 20
[class] = > English course
)
Left connection
Contains all rows that make the table (regardless of whether there are matching rows in the right table), and all matching rows in the right table.
Format:
SELECT A.c1, B.c2 FROM A LEFT JOIN B ON A.c3 = B.c3
Example:
Output result:
Database link successful
The SQL statement was executed successfully!
Array
(
[name] = > Yang Fugui
[age] = > 18
[class] = > math class
)
Array
(
[name] = > Yang Mei
[age] = > 19
[class] = > Chinese course
)
Array
(
[name] = > Yang Jianguo
[age] = > 20
[class] = > English course
)
Array
(
[name] > Yang Tiezhu
[age] = > 21
[class] = >
)
Right connection
Contains all rows of the right table (regardless of whether there are matching rows in the left table) and all matching rows in the left table.
Format:
SELECT A.c1, B.c2 FROM A RIGHT JOIN B ON A.c3 = B.c3
Example:
Output result:
Database link successful
The SQL statement was executed successfully!
Array
(
[name] = > Yang Fugui
[age] = > 18
[class] = > math class
)
Array
(
[name] = > Yang Mei
[age] = > 19
[class] = > Chinese course
)
Array
(
[name] = > Yang Jianguo
[age] = > 20
[class] = > English course
)
Array
(
[name] = >
[age] = >
[class] = > History course
)
Full connection
Contains all rows of the left and right tables (regardless of whether there is a matching row in the other table or not).
Format:
SELECT A.c1, B.c2 FROM A LEFT JOIN B ON A.c3 = B.c3
UNION
SELECT A.c1, B.c2 FROM A RIGHT JOIN B ON A.c3 = B.c3
Example:
Output result:
Database link successful
The SQL statement was executed successfully!
Array
(
[name] = > Yang Fugui
[age] = > 18
[class] = > math class
)
Array
(
[name] = > Yang Mei
[age] = > 19
[class] = > Chinese course
)
Array
(
[name] = > Yang Jianguo
[age] = > 20
[class] = > English course
)
Array
(
[name] > Yang Tiezhu
[age] = > 21
[class] = >
)
Array
(
[name] = >
[age] = >
[class] = > History course
)
Thank you for reading! This is the end of this article on "how to use table joins in MySQL and PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!