2 回答
TA贡献2003条经验 获得超2个赞
您需要对adhesives源表中的每一列进行一个连接,如下所示:
SELECT
s.sheet_color,
s.sheet_code,
s.factor_1,
s.factor_2,
sb.brand_name,
a1.match_code match_code_1,
a1.match_name match_name_1,
a2.match_code match_code_2,
a2.match_name match_name_2,
a3.match_code match_code_3,
a3.match_name match_name_3
FROM sheets s
LEFT JOIN sheet_brands sb ON sb.brand_id = s.brand_id
LEFT JOIN adhesives a1 ON a1.adhesive_id = s.adhesive_id_1
LEFT JOIN adhesives a2 ON a2.adhesive_id = s.adhesive_id_2
LEFT JOIN adhesives a3 ON a3.adhesive_id = s.adhesive_id_3
TA贡献1829条经验 获得超6个赞
您必须加入表adhesives两次才能同时获得match_codes 和match_names:
SELECT s.sheet_color, s.sheet_code, s.factor_1, s.factor_2,
b.brand_name,
a1.match_code match_code1, a1.match_name match_name1,
a2.match_code match_code2, a2.match_name match_name2
FROM sheets s
LEFT JOIN sheet_brands b ON b.brand_id = s.brand_id
LEFT JOIN adhesives a1 ON a1.adhesive_id = s.adhesive_id_1
LEFT JOIN adhesives a2 ON a2.adhesive_id = s.adhesive_id_2
为表使用别名并使用这些别名限定列名。
如果您还想要并且match_code因为match_name您adhesive_id_3将需要再加入一个:
SELECT s.sheet_color, s.sheet_code, s.factor_1, s.factor_2,
b.brand_name,
a1.match_code match_code1, a1.match_name match_name1,
a2.match_code match_code2, a2.match_name match_name2,
a3.match_code match_code3, a3.match_name match_name3
FROM sheets s
LEFT JOIN sheet_brands b ON b.brand_id = s.brand_id
LEFT JOIN adhesives a1 ON a1.adhesive_id = s.adhesive_id_1
LEFT JOIN adhesives a2 ON a2.adhesive_id = s.adhesive_id_2
LEFT JOIN adhesives a3 ON a3.adhesive_id = s.adhesive_id_3
- 2 回答
- 0 关注
- 111 浏览
添加回答
举报