选择多个元组为一个

时间:2013-03-28 14:16:13

标签: mysql sql select

SELECT contact_id, name, email, phone, city 
FROM ak_contact
WHERE email = 'test@gmail.com'
ORDER BY contact_id DESC

Tis查询返回类似这样的内容:

contact_id  name       email     phone      city

  8499  Serj    test@gmail.com      
  8498  Serj    test@gmail.com  3-33-333    
  8494  Serj    test@gmail.com              London
  8493  Serj    test@gmail.com  2-22-222    

但是我需要一个元组作为结果只包含最新值(如果它们不是null /空) 所以我需要的结果应该是这样的:

  contact_id name   email           phone       city

      8499  Serj    test@gmail.com  3-33-333    London

2 个答案:

答案 0 :(得分:3)

我通常不喜欢嵌套的select语句,但这是在MySQL中解决这个问题的一种方法:

select (select contact_id
        from ak_contact
        where email = 'test@gmail.com' and
              contact_id is not null
        order by contact_id desc
        limit 1
       ) as contact_id,
       (select name
        from ak_contact
        where email = 'test@gmail.com' and
              name is not null
        order by contact_id desc
        limit 1
       ) as name,
       (select phone
        from ak_contact
        where email = 'test@gmail.com' and
              phone is not null
        order by contact_id desc
        limit 1
       ) as phone,
       (select city
        from ak_contact
        where email = 'test@gmail.com' and
              city is not null
        order by contact_id desc
        limit 1
       ) as city

每列可能来自不同的行,因此每个列都有自己的查询。

答案 1 :(得分:3)

更像戈登的答案......

SELECT a.c_id, 
       CASE 
         WHEN b.name IS NULL THEN (SELECT name 
                                   FROM   ak_contact 
                                   WHERE  name IS NOT NULL 
                                          AND email = a.email 
                                   ORDER  BY c_id DESC 
                                   LIMIT  1) 
         ELSE b.name 
       end AS name, 
       b.email, 
       CASE 
         WHEN b.phone IS NULL THEN (SELECT phone 
                                    FROM   ak_contact 
                                    WHERE  phone IS NOT NULL 
                                           AND email = a.email 
                                    ORDER  BY c_id DESC 
                                    LIMIT  1) 
         ELSE b.phone 
       end AS phone, 
       CASE 
         WHEN b.city IS NULL THEN (SELECT city 
                                   FROM   ak_contact 
                                   WHERE  city IS NOT NULL 
                                          AND email = a.email 
                                   ORDER  BY c_id DESC 
                                   LIMIT  1) 
         ELSE b.city 
       end AS city 
FROM   (SELECT email, 
               Max(c_id) AS c_id 
        FROM   ak_contact 
        WHERE  email = 'test@gmail.com' 
        GROUP  BY email) a 
       LEFT JOIN ak_contact b 
              ON b.c_id = a.c_id 

<强>结果

| C_ID | NAME |          EMAIL |    PHONE |   CITY |
----------------------------------------------------
| 8499 | Serj | test@gmail.com | 3-33-333 | London |

See the demo