O'Reilly logo

Sams Teach Yourself MySQL by Chris Newman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 | +----------+------------+-------------+--------------------+ | ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required