我有一张名为' quote'它有以下字段..
category_id
subcategory_id
country_id
region_id
rural_id
notes
sender_uid
receiver_uid
created_timestamp
is_quote_active
此外,我还有'类别','子类别',' country',' region',' rural'和'用户'表。 我需要一个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
事实上,我需要获取名称而不是ID。
我正在寻找最有效的方式..
由于
答案 0 :(得分: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'
答案 1 :(得分: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