亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將同一表中的 2 列匹配到另一個表中的同一列(總共 3 個連接)

將同一表中的 2 列匹配到另一個表中的同一列(總共 3 個連接)

PHP
弒天下 2023-03-04 17:07:02
我有 3 張桌子。sheets TABLEsheet_id brand_id sheet_color sheet_code adhesive_id_1 adhesive_id_2 adhesive_id_3 factor_1 factor_2 factor_3sheet_brands TABLE brand_id brand_name surface_type_idadhesives TABLE adhesive_id match_code match_name我需要匹配:1) sheets.brand_id 到 sheet_brands.brand_id 得到brand_name2) sheets.adhesive_id_1 to adhesives.adhesive_id 得到match_code和match_name3) sheets.adhesive_id_2 to adhesives.adhesive_id 得到match_code和match_name我花了一段時間搜索,但沒有找到有效的解決方案。我相信這種類型的 JOIN 是獨一無二的,因為我需要將同一個表中的 2 列匹配到另一個表中的同一列。這是我最接近解決問題的時間,但它只匹配第一個 adhesive_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";
查看完整描述

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


查看完整回答
反對 回復 2023-03-04
?
肥皂起泡泡

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


查看完整回答
反對 回復 2023-03-04
  • 2 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號