-3
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/empty)所以我需要的结果应该是这样的:

  contact_id name   email           phone       city

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

2 回答 2

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

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

于 2013-03-28T14:26:12.130 回答
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 | 姓名 | 电子邮件 | 电话 | 城市 |
-------------------------------------------------- --
| 8499 | 服务 | test@gmail.com | 3-33-333 | 伦敦 |

查看演示

于 2013-03-28T14:34:37.180 回答