2014-09-29 126 views
1

我有以下表格。MySql內部連接查詢7個表

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 ¦ 
--------------------------------------- 

我正在一個web應用程序中,我有這7個互相連接的表。 我需要一個PHP MYSQL連接查詢將列出已經以表格格式已經下令所有汽車行駛下列:

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

請幫助!

+2

你試圖達到這樣的結果的地方在哪裏? – 2014-09-29 09:15:16

+0

請注意,order_items中的sn列(可能是)多餘的 – Strawberry 2014-09-29 09:33:19

+0

我是mysql新手。我可以寫簡單的querries。我試圖用嵌套querry實現這一點,但它並沒有工作......這對我來說是一種火箭科學 – user1362473 2014-09-29 09:52:09

回答

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

謝謝。它的工作除了「汽車訂購」欄。汽車有序列在所有行中顯示「0」零。此列應顯示「母品牌,子品牌,顏色」即「Nissa,陽光明媚,綠色」 – user1362473 2014-10-01 10:06:50

+0

請再試一次 – jainvikram444 2014-10-01 10:52:29

+0

以前的例子是MS-SQL服務器,現在加入concat函數加入 – jainvikram444 2014-10-01 10:53:26