数据透视表加入数据问题

时间:2018-03-13 05:17:30

标签: mysql inner-join pivot-table

我有以下MYSQL表

   fieldName    fieldData   businessUnitId    clientId
    SJM ID       123456      1                 1
    Username     abcdedg     1                 1
    First Name   Danny       1                 1
    Last Name    Younes      1                 1
    Email        test@t.com  1                 1
    SJM ID       123456      4                 1
    Dealer Code  123456      4                 1
    Dealer       South Port  4                 1 

我想将所有字段与不同的业务单位合并

我有以下SQL查询

SELECT 
MAX(IF(sso3.fieldName = 'SJM ID', sso3.fieldData, NULL)) AS 'SJMID', 
MAX(IF(sso3.fieldName = 'Username', sso3.fieldData, NULL)) AS 'USERNAME', 
MAX(IF(sso3.fieldName = 'First Name', sso3.fieldData, NULL)) AS 'FIRST NAME', 
MAX(IF(sso3.fieldName = 'Last Name', sso3.fieldData, NULL)) AS 'LAST NAME', 
MAX(IF(sso2.fieldName = 'Dealer Code', sso2.fieldData, NULL)) AS 'DCODE',
MAX(IF(sso3.fieldName = 'Email', sso3.fieldData, NULL)) AS 'Email' 
FROM mi_ssousers2 sso2 inner join mi_ssousers2 sso3 on sso3.clientId=sso2.clientId 
WHERE sso2.businessUnitId=4 and sso3.businessUnitId=1 group by sso2.clientId

但它给我以下结果,我从表中的其他记录得到随机结果

SJMID     USERNAME      FIRST NAME    LAST NAME    DCODE    Email
123456    RANDON VALUE  RANDOM VALUE  RANDOM VALUE 123456   RANDOM VALUE

预期输出如下

SJM ID     USERNAME    FIRST NAME     LAST NAME    DCODE     EMAIL
123456     abcdefg     Danny          Younes       123456    test@t.com

由于 丹尼

0 个答案:

没有答案