Mysql查询INNER JOIN

时间:2016-10-18 14:16:30

标签: mysql

我有以下2个表格:

LEADid
pmp
pms
ci_lead
av_lead
prod_lead
f_lead
sec_lead
pid

TeamMember

UID
fname
lname
email
d_phone
h_phone

Lead表中的所有数据都是int(),对应于TeamMember表中的UID。

所以我要做的是,比如说我使用PID作为我的密钥从Lead表中选择一行,我想从TeamMembers中提取所有匹配来自ID的id的fname和lname表。我已经尝试过INNER Joins来完成这个,但似乎总是只输出第一个id。

这是我试过的最新查询

SELECT `TeamMember`.`fname`,
       `TeamMember`.`lname`
  FROM `MaxusDash`.`Lead`
 INNER JOIN `MaxusDash`.`TeamMember`
    ON (`Lead`.`pmp` = `TeamMember`.`UID`)
   AND (`Lead`.`pms` = `TeamMember`.`UID`)
   AND (`Lead`.`ci_lead` = `TeamMember`.`UID`)
   AND (`Lead`.`av_lead` = `TeamMember`.`UID`)
   AND (`Lead`.`prod_lead` = `TeamMember`.`UID`)
   AND (`Lead`.`f_lead` = `TeamMember`.`UID`)
   AND (`Lead`.`sec_lead` = `TeamMember`.`UID`)
 WHERE `Lead`.`pid` = 44;

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您已使用UNION或为每个潜在客户

创建JOIN

<强> UNION:

SELECT `pmp` as lead, `TeamMember`.`fname`, `TeamMember`.`lname`
FROM `MaxusDash`.`Lead`
INNER JOIN `MaxusDash`.`TeamMember` 
        ON (`Lead`.`pmp` = `TeamMember`.`UID`) 
WHERE `Lead`.`pid`=44
UNION  
SELECT `pms` as lead, `TeamMember`.`fname`, `TeamMember`.`lname`
FROM `MaxusDash`.`Lead`
INNER JOIN `MaxusDash`.`TeamMember` 
        ON (`Lead`.`pms` = `TeamMember`.`UID`) 
WHERE `Lead`.`pid`=44
UNION  
....
UNION
SELECT `sec_lead` as lead, `TeamMember`.`fname`, `TeamMember`.`lname`
FROM `MaxusDash`.`Lead`
INNER JOIN `MaxusDash`.`TeamMember` 
        ON (`Lead`.`sec_lead` = `TeamMember`.`UID`) 
WHERE `Lead`.`pid`=44

<强> JOIN

SELECT `pmp`.`fname`, `pmp`.`lname`,
       `psp`.`fname`, `psp`.`lname`,
       ....
       `sec_lead`.`fname`, `sec_lead`.`lname`
FROM `MaxusDash`.`Lead` as `L`
INNER JOIN `MaxusDash`.`TeamMember` as `pmp`
        ON L.`pmp` = `pmp`.`UID`

INNER JOIN `MaxusDash`.`TeamMember` as `psp`
        ON L.`psp` = `psp`.`UID`

....
INNER JOIN `MaxusDash`.`TeamMember` as `sec_lead`
        ON L.`sec_lead` = `sec_lead`.`UID`

WHERE `L`.`pid`=44

答案 1 :(得分:0)

如果要返回单行上的所有名称,则需要对每个外键关系使用单独的连接操作。

但似乎你想要在一个单独的行上返回每个名字。

一种方法是这样的:

 SELECT r.role
      , m.fname
      , m.lname
   FROM `MaxusDash`.`Lead` l
  CROSS
   JOIN (           SELECT 'pmp' AS `role`
          UNION ALL SELECT 'pms'
          UNION ALL SELECT 'ci_lead'
          UNION ALL SELECT 'av_lead'
          UNION ALL SELECT 'prod_lead'
          UNION ALL SELECT 'f_lead'
          UNION ALL SELECT 'sec_lead'
        ) r
   LEFT     
   JOIN `MaxusDash`.`TeamMember` m
     ON ( m.uid = l.pmp       AND r.role = 'pmp'       )
     OR ( m.uid = l.pms       AND r.role = 'pms'       )
     OR ( m.uid = l.ci_lead   AND r.role = 'ci_lead'   )
     OR ( m.uid = l.av_lead   AND r.role = 'av_lead'   )
     OR ( m.uid = l.prod_lead AND r.role = 'prod_lead' )
     OR ( m.uid = l.f_lead    AND r.role = 'f_lead'    )
     OR ( m.uid = l.sec_lead  AND r.role = 'sec_lead'  )

  WHERE l.pid = 44

根据role列的字符集和您的连接,您可能需要指定(覆盖默认值)字符串文字的字符集。例如,如果rolelatin1,并且您的连接为UTF8,为了避免“非法混合排序”错误,您可以在 _latin1 <前面添加字符串文字/强>

                    SELECT _latin1'pmp' AS `role`
          UNION ALL SELECT _latin1'pms'

如果我们不需要返回该值,请从SELECT列表中删除r.role表达式。删除LEFT关键字以使用内部联接而不是外部联接。