简体   繁体   中英

Single row result of join sql query

I have an sql query :

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 ) ;

This returns result in 2 rows are there are 2 enum type of account type :

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

But i want accountnumber to be displayed in single line as savingaccountnumber and checkingaccountnumber based on accounttype.

Expected Result:

+--------------+-----------+----------+----------------+------------+---------------------+-----------------------+
| id           | firstname | lastname | emailcollected | phone      | savingaccountnumber | checkingaccountnumber |
+--------------+-----------+----------+----------------+------------+---------------------+-----------------------+
|            9 | dineshND  | NineND   |     1536064455 | 5175761111 | 102323              |            102324     |
+--------------+-----------+----------+----------------+------------+---------------------+-----------------------+
  • savingaccountnumber column when accounttype is 5003
  • checkingaccountnumber column when accountype is 5002
  • My Mysql version : mysql Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2

From your sample data and expected result you can try to use condition aggravated function

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

You need to join on bankaccountinfo twice to get things in one row: once for checking and once for savings. Assuming you (a) want to return a row even if the account records are missing, and (b) that there is a unique key defined on bankaccountinfo for 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)

Either way, brush up on what JOIN and IN do to understand why you got two results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM