Inner join 3 table mysql

Last update on August 19 2022 21:50:42 [UTC/GMT +8 hours]

What is INNER JOIN in MySQL?

In MySQL the INNER JOIN selects all rows from both participating tables to appear in the result if and only if both tables meet the conditions specified in the ON clause. JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents. In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.

Pictorial presentation of MySQL INNER JOIN :

MySQL INNER JOIN Syntax:

MySQL supports the following JOIN syntaxes for the table_references [A table reference is also known as a join expression.] part of SELECT statements and multiple-table UPDATE and DELETE statements:

table_references:
    escaped_table_reference [, escaped_table_reference] ...

escaped_table_reference:
    table_reference
  | { OJ table_reference }

table_reference:
    table_factor
  | join_table

table_factor:
    tbl_name [PARTITION [partition_names]] 
        [[AS] alias] [index_hint_list]
  | table_subquery [AS] alias
  | [ table_references ]

join_table:
    table_reference [INNER | CROSS] JOIN table_factor [join_condition]
  | table_reference STRAIGHT_JOIN table_factor
  | table_reference STRAIGHT_JOIN table_factor ON conditional_expr
  | table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
  | table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor

join_condition:
    ON conditional_expr
  | USING [column_list]

index_hint_list:
    index_hint [, index_hint] ...

index_hint:
    USE {INDEX|KEY}
      [FOR {JOIN|ORDER BY|GROUP BY}] [[index_list]]
  | IGNORE {INDEX|KEY}
      [FOR {JOIN|ORDER BY|GROUP BY}] [index_list]
  | FORCE {INDEX|KEY}
      [FOR {JOIN|ORDER BY|GROUP BY}] [index_list]

index_list:
    index_name [, index_name] ...

Example : MySQL INNER JOIN

When combining records from more than one tables, an user needs to indicate how records in a table can be matched to records in the other. As the both of tables have a cate_id column, we can match using that column. The ON clause is used to match records in two tables, based on the value of cate_id column. Usage of INNER JOIN combines the tables. An INNER JOIN allows rows from either table to appear in the result if and only if both tables meet the conditions specified in the ON clause.

In this example, the ON clause specifies that the cate_id column of both book_mast and category table must match. If a cate_id does not appear in both of the tables, the row will not appear in the result because the condition in the ON clause fails. Only those categories will participate in the JOIN whose books are written in ENGLISH.

Code:

SELECT book_mast.book_id,book_mast.book_name,cate_descrip           
FROM book_mast 
INNER JOIN category          
ON book_mast.cate_id=category.cate_id        
WHERE book_mast.pub_lang="English";

Relational Algebra Expression:

Relational Algebra Tree:

Sample table: book_mast

Sample table: category

Sample Output:

mysql> SELECT book_mast.book_id,book_mast.book_name,cate_descrip           
    -> FROM book_mast 
    -> INNER JOIN category          
    -> ON book_mast.cate_id=category.cate_id        
    -> WHERE book_mast.pub_lang="English";
+---------+-------------------------------------+--------------+
| book_id | book_name                           | cate_descrip |
+---------+-------------------------------------+--------------+
| BK001   | Introduction to Electrodynamics     | Science      | 
| BK002   | Understanding of Steel Construction | Technology   | 
| BK004   | Transfer  of Heat and Mass          | Technology   | 
| BK010   | Fundamentals of Thermodynamics      | Technology   | 
| BK012   | The Nature  of World                | Nature       | 
| BK009   | Mental Health Nursing               | Medical      | 
+---------+-------------------------------------+--------------+
6 rows in set [0.04 sec]

PHP script:






example-inner-join-with-multiple-tables php mysql examples | w3resource




List of the book ids, name of the book and category description:

Book IDName of the bookCategory description

Bài mới nhất

Chủ Đề