带有7个表的MySql内部连接查询

时间:2014-09-29 09:13:40

标签: mysql inner-join

我有以下表格。

cars
------------------------------------
¦ car_id  ¦ color_id  ¦ brand_id   ¦
------------------------------------
¦ 1       ¦ 1         ¦ 3          ¦
¦ 2       ¦ 2         ¦ 3          ¦
¦ 3       ¦ 1         ¦ 4          ¦
¦ 4       ¦ 3         ¦ 4          ¦
------------------------------------
colors
-----------------------
¦ color_id¦ color     ¦
-----------------------
¦ 1       ¦ red       ¦
¦ 2       ¦ blue      ¦
¦ 3       ¦ green     ¦
¦ 4       ¦ white     ¦
-----------------------
brands
-----------------------------------
¦ brand_id¦ brand    ¦ parent_id  ¦
-----------------------------------
¦ 1       ¦ Toyota   ¦ 0          ¦
¦ 2       ¦ Nissan   ¦ 0          ¦
¦ 3       ¦ Sunny    ¦ 2          ¦
¦ 4       ¦ Corrola  ¦ 1          ¦
-----------------------------------
companies
---------------------------------
¦ company_id¦ name  ¦ location  ¦
---------------------------------
¦ 1         ¦ ABC   ¦ New York  ¦
¦ 2         ¦ XYZ   ¦ Florida   ¦
¦ 3         ¦ ASD   ¦ Texas     ¦
¦ 4         ¦ 3MM   ¦ Florida   ¦
---------------------------------
contacts
-----------------------------------
¦ contact_id¦ name   ¦ company_id ¦
-----------------------------------
¦ 1         ¦ James  ¦ 1          ¦
¦ 2         ¦ Kevin  ¦ 1          ¦
¦ 3         ¦ Mic    ¦ 2          ¦
¦ 4         ¦ Nadia  ¦ 3          ¦
-----------------------------------
orders
---------------------------------------
¦ order_id  ¦ company_id ¦ contact_id ¦
---------------------------------------
¦ 1         ¦ 1          ¦ 1          ¦
¦ 2         ¦ 1          ¦ 2          ¦
¦ 3         ¦ 2          ¦ 3          ¦
¦ 4         ¦ 3          ¦ 4          ¦
---------------------------------------
order_items
---------------------------------------
¦ sn ¦ order_id  ¦ car_id    ¦ price  ¦
---------------------------------------
¦ 1  ¦ 1         ¦ 1         ¦ 100    ¦
¦ 2  ¦ 2         ¦ 2         ¦ 200    ¦
¦ 3  ¦ 3         ¦ 3         ¦ 100    ¦
¦ 4  ¦ 3         ¦ 4         ¦ 150    ¦
---------------------------------------

我正在开发一个网络应用程序,其中我有7个相互连接的表。 我需要一个PHP MYSQL JOIN查询,它将列出以表格形式订购的所有汽车,其中包含以下列:

--------------------------------------------------------------------------
¦ Order ID ¦ Company Name  ¦ Contact Name ¦ Car Ordered         ¦ price  ¦
--------------------------------------------------------------------------
¦ 1        ¦ ABC           ¦ James        ¦ Nissan Sunny, Green ¦ 100    ¦
--------------------------------------------------------------------------

请帮助!!

1 个答案:

答案 0 :(得分:0)

Please try with this query:

SELECT  orders.order_id as 'Order ID',
     companies.name as  'Company Name',
     contacts.name as 'Contact Name ', 
     CONCAT( brands.brand , ', ' ,colors.colcor) as 'Car Ordered',
     order_items.price as 'price'
     FROM order_items  
    left outer join orders on orders.order_id = order_items.order_id
    left outer join contacts on orders.contact_id = contacts.contact_id 
    left outer join companies on orders.company_id  = companies.company_id
    left outer join cars on order_items.car_id =cars.car_id  
    left outer join colors on cars.color_id = colors.color_id
    left outer join brands on cars.brand_id = brands.brand_id
    order by brands.brand asc