我有两张表,如图1所示:
我需要这个结果:
我尝试使用连接,但我无法获得所需的结果。
SELECT
company_info.t_id,
company_info.company_name,
company_info.remark,
tender.company_selected,
company_info.company_document
FROM
company_info
LEFT OUTER JOIN
tender
ON company_info.t_id = tender.t_id;
注意:它在两行中都显示公司文档,但我只需要选择公司。请查看快照2以供参考。
答案 0 :(得分:0)
SELECT
company_info.t_id,
company_info.company_name,
company_info.remark,
tender.company_selected,
company_info.company_document
FROM
company_info
LEFT OUTER JOIN
tender
ON company_info.t_id = tender.t_id;
答案 1 :(得分:0)
left join
应该可以解决问题:
SELECT a.t_id a.company_name, a.remark,
b.company_selected, b.company_document
FROM company_info a
LEFT JOIN tender b ON a.t_id = b.t_id
答案 2 :(得分:0)
这将为您提供所需的信息:
SELECT c.t_id,
c.company_name,
c.remark,
IFNULL(t.company_selected,'') as company_selected,
CASE WHEN t.company_selected IS NULL THEN '' ELSE c.company_document END AS company_document
FROM company_info c LEFT JOIN
tender t ON c.t_id=t.t_id AND c.company_name=t.company_selected
<强>解释强>
此查询将执行以下操作:
t_id
,comapany_name
,remark
。company_document
。答案 3 :(得分:0)
此代码有效
SELECT company_info.t_id, company_info.company_name, company_info.remark,
CASE WHEN tender.company_selected =company_info.company_name THEN tender.company_selected ELSE '' END,
CASE WHEN tender.company_selected =company_info.company_name THEN company_info.company_document ELSE '' END FROM
company_info LEFT OUTER JOIN tender ON company_info.t_id = tender.t_id