【发布时间】:2020-08-25 23:00:49
【问题描述】:
我有 3 张桌子。
sheets TABLE
sheet_id
brand_id
sheet_color
sheet_code
adhesive_id_1
adhesive_id_2
adhesive_id_3
factor_1
factor_2
factor_3
sheet_brands TABLE
brand_id
brand_name
surface_type_id
adhesives TABLE
adhesive_id
match_code
match_name
我需要匹配:
1) sheet.brand_id 到 sheet_brands.brand_id 得到brand_name
2)sheets.adhesive_id_1 到adhesives.adhesive_id 得到match_code 和match_name
3) sheet.adhesive_id_2 到adhesives.adhesive_id 获取match_code 和match_name
我已经搜索了一段时间,但没有找到可行的解决方案。我相信这种类型的 JOIN 是独一无二的,因为我需要将同一个表中的 2 列匹配到另一个表中的同一列。
这是我最接近解决问题的方法,但它只匹配第一个粘合剂_id_1。它与adhesive_id_2 或adhesive_id_3 不匹配。
$sql = "SELECT sheet_color, sheet_code, factor_1, factor_2, brand_name, match_code, match_name FROM sheets LEFT JOIN sheet_brands ON sheet_brands.brand_id = sheets.brand_id LEFT JOIN adhesives ON adhesives.adhesive_id = sheets.adhesive_id_1";
【问题讨论】:
-
您选择接受最新答案有什么原因吗?
标签: php mysql sql join left-join