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

このクエリは、次のような結果を返します。

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    

しかし、最新の値のみを含む結果として 1 つのタプルが必要です (null/empty でない場合)。したがって、必要な結果は次のようになります。

  contact_id name   email           phone       city

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

2 に答える 2

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 に答える
3

私は通常、ネストされたselectステートメントのファンではありませんが、これは MySQL でこれにアプローチする 1 つの方法です。

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 に答える