Joining Multiple Tables

To join multiple tables using the JOIN ... ON syntax, each table has its own JOIN keyword and its own ON clause immediately afterward.

The following example reproduces the query from Lesson 11 that retrieves the order history for a given customer, joining the orders,order_lines, and products tables:

mysql> SELECT o.order_id, o.order_date, l.quantity, p.name
    -> FROM orders o
    -> INNER JOIN order_lines l
    -> ON o.order_id = l.order_id
    -> INNER JOIN products p
    -> ON p.product_code = l.product_code
    -> WHERE o.customer_code ='SCICORP'
    -> ORDER BY o.order_date; +----------+------------+-------------+--------------------+ | order_id | order_date | quantity | name | +----------+------------+-------------+--------------------+ | ...

Get Sams Teach Yourself MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.