2014-03-25 45 views
0

我有一個表命名爲「報價」,它具有以下字段..我的SQL加入超過5桌

category_id 
subcategory_id 
country_id 
region_id 
rural_id 
notes 
sender_uid 
receiver_uid 
created_timestamp 
is_quote_active 

我也有「類別」,「子類別」,「全國」,「區域」 ,'農村'和'用戶'表。 我需要一個SQL查詢以檢索數據如下..

category_name (from 'category' table) 
subcategory_name (from 'subcategory' table) 
country_name (from 'country' table) 
region_name (from 'region' table) 
rural_name (from 'rural' table) 
notes 
sender_name (from 'user' table) 
receiver_name (from 'user' table) 
created_timestamp 
is_quote_active 

其實我需要的名稱,而不是標識。 我在尋找最effecient方式..

感謝

+0

那麼問題是什麼?超過5個表格與2個表格完全相同。 – zerkms

回答

1

試試這個,

SELECT quote.quote_id, quote.notes, quote.is_quote_active, quote.created_timestamp, category.category_name, subcategory.subcategory_name, rural.rural_name, region.region_name, country.country_name, s.name AS sname, r.name AS rname 
    FROM quote 
     LEFT JOIN category ON quote.category_id = category.category_id 
    LEFT JOIN subcategory ON quote.subcategory_id = subcategory.subcategory_id 
    LEFT JOIN country ON quote.country_id = country.country_id 
    LEFT JOIN region ON quote.region_id = region.region_id 
    LEFT JOIN rural ON quote.rural_id = rural.rural_id 
    LEFT JOIN user s ON quote.sender_uid = s.uid 
    LEFT JOIN user r ON quote.receiver_uid = r.uid 
2

你需要加入所有這些表

例如,

SELECT CN.category_name ,SC.subcategory_name ,CO.country_name ,R.region_name ,RE.region_name ,U.sender_name ,U.receiver_name 
From category Left JOIN subcategory ON ... 
       Left JOIN country ON.. 
       Left JOIN region ON.. 
       Left JOIN rural ON.. 
       Left JOIN User ON.. 
Where 'Your condition here'