【问题标题】:Match 2 columns from the same table to the same single column in another table (total of 3 joins)将同一表中的 2 列匹配到另一个表中的同一列(总共 3 个连接)
【发布时间】: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


【解决方案1】:

您需要对源表中的每一列 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

【讨论】:

  • 谢谢!这在 phpMyAdmin 中执行时有效,但是当我在 PHP 中浏览我的行时,我将如何访问 a1.match_code、a2.match_code 等。我尝试了 $row['a1.match_code']、$row['match_code'] 等。没有结果。适用于 $row['brand_name']、$row['sheet_color'] 等。
  • @user1822824:您需要为列添加别名 - 只需将其添加到查询中。然后您可以使用$row['match_code_1'] 等方式访问它们。
【解决方案2】:

您必须加入表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_codematch_nameadhesive_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

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2023-03-19
    • 2011-11-11
    • 2010-10-20
    • 2018-12-14
    • 1970-01-01
    相关资源
    最近更新 更多