简体   繁体   English

我的SQL连接超过5个表

[英]My SQL Join more than 5 tables

I Have a table named 'quote' and it has following fields.. 我有一个名为“ quote”的表,它具有以下字段。

category_id
subcategory_id
country_id
region_id
rural_id
notes
sender_uid
receiver_uid 
created_timestamp
is_quote_active

Also I have 'category','subcategory','country','region','rural' and 'user' tables. 我也有“类别”,“子类别”,“国家”,“地区”,“农村”和“用户”表。 I need an SQL query to retrive data as follows.. 我需要一个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

in fact I need to get names instead of IDs. 实际上,我需要获取名称而不是ID。 I'm looking for the most effecient way.. 我正在寻找最有效的方法。

Thanks 谢谢

You need to Join All these table 您需要加入所有这些表

For example, 例如,

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'

Try this, 尝试这个,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM