连接SQL查询的单行结果

时间:2018-12-13 14:33:14

标签: mysql sql join select

我有一个SQL查询:

SELECT p.id, p.firstname, p.lastname, p.emailcollected, p.phone, e.accountid, e.accounttype
FROM player p
JOIN bankaccountinfo e ON p.id = e.playerid
WHERE ( ( p.createtime > 0 AND p.createtime < 2000000000 ) OR
        ( p.updatetime > 0 AND p.updatetime < 200000000000000 ) )
  AND e.accounttype IN ( 5002,5003 ) ;

这将返回2行的结果,其中有2种枚举类型的帐户类型:

+--------------+-----------+----------+----------------+------------+---------------+-------------+
| id           | firstname | lastname | emailcollected | phone      | accountnumber | accounttype |
+--------------+-----------+----------+----------------+------------+---------------+-------------+
|            9 | dineshND  | NineND   |     1536064455 | 5175761111 | 102323        |        5002 |
|            9 | dineshND  | NineND   |     1536064455 | 5175761111 | 102324        |        5003 |
+--------------+-----------+----------+----------------+------------+---------------+-------------+

但是我希望根据帐号类型将帐号作为保存帐号和支票帐号单行显示。

预期结果:

+--------------+-----------+----------+----------------+------------+---------------------+-----------------------+
| id           | firstname | lastname | emailcollected | phone      | savingaccountnumber | checkingaccountnumber |
+--------------+-----------+----------+----------------+------------+---------------------+-----------------------+
|            9 | dineshND  | NineND   |     1536064455 | 5175761111 | 102323              |            102324     |
+--------------+-----------+----------+----------------+------------+---------------------+-----------------------+
  • 帐户类型为5003时的Savingaccountnumber列
  • 帐户类型为5002时检查帐户号列
  • 我的Mysql版本:适用于使用readline 5.2的debian-linux-gnu(i486)的mysql Ver 14.12 Distrib 5.0.51a

2 个答案:

答案 0 :(得分:2)

根据样本数据和预期结果,您可以尝试使用条件加重功能

SELECT id,
    firstname,
    lastname,
    emailcollected,
    phone,
    MAX(CASE WHEN accounttype = 5003 THEN accountid END) savingaccountnumber,
    MAX(CASE WHEN accounttype = 5002 THEN accountid END) checkingaccountnumber
FROM (
    SELECT p.id, p.firstname, p.lastname, p.emailcollected, p.phone, e.accountid, e.accounttype
    FROM player p
    JOIN bankaccountinfo e ON p.id = e.playerid
    WHERE ( ( p.createtime > 0 AND p.createtime < 2000000000 ) OR
            ( p.updatetime > 0 AND p.updatetime < 200000000000000 ) )
      AND e.accounttype IN ( 5002,5003 ) 
) t1 
GROUP BY  id,firstname,lastname,emailcollected,phone

答案 1 :(得分:1)

您需要两次加入bankaccountinfo才能将内容排成一行:一次用于检查,一次用于节省。假设您(a)即使缺少帐户记录也要返回一行,并且(b)在bankaccountinfo上为playerid, accounttype定义了唯一键:

SELECT
    p.id,
    p.firstname,
    p.lastname,
    p.emailcollected,
    p.phone,
    e1.accountid checkingaccountnumber,
    e2.accountid savingaccountnumber
FROM
    player p
LEFT JOIN
    bankaccountinfo e1
ON
    p.id = e1.playerid
    AND e1.accounttype = 5002
LEFT JOIN
    bankaccountinfo e2
ON
    p.id = e2.playerid
    AND e2.accounttype = 5003
WHERE
    (p.createtime > 0 AND p.createtime < 2000000000)
    OR (p.updatetime > 0 AND p.updatetime < 200000000000000)

无论哪种方式,都要仔细研究JOIN和IN的作用,以了解为什么会得到两个结果。