-3
SELECT contact_id, name, email, phone, city 
FROM ak_contact
WHERE email = '[email protected]'
ORDER BY contact_id DESC

Tis query returns me something like this:

contact_id  name       email     phone      city

  8499  Serj    [email protected]      
  8498  Serj    [email protected]  3-33-333    
  8494  Serj    [email protected]              London
  8493  Serj    [email protected]  2-22-222    

But I need one tuple as a result containing only latest values ( if they are not null/empty ) So the result I need should looks like this:

  contact_id name   email           phone       city

      8499  Serj    [email protected]  3-33-333    London
3
  • 2
    so what do you mean by LATEST? how can you tell that the record is the latest among the group?
    – John Woo
    Mar 28, 2013 at 14:20
  • 2
    Can you elaborate on what you mean by latest? Or even expand your data set, show the full table structure.
    – Taryn
    Mar 28, 2013 at 14:27
  • Guys, if you can't get the sense of the question it's not the reason to vote against it. It's not my fault that the database has the stupid structure! Jan 10, 2016 at 8:18

2 Answers 2

3

I'm not generally a fan of nested select statements, but that is one way to approach this in MySQL:

select (select contact_id
        from ak_contact
        where email = '[email protected]' and
              contact_id is not null
        order by contact_id desc
        limit 1
       ) as contact_id,
       (select name
        from ak_contact
        where email = '[email protected]' and
              name is not null
        order by contact_id desc
        limit 1
       ) as name,
       (select phone
        from ak_contact
        where email = '[email protected]' and
              phone is not null
        order by contact_id desc
        limit 1
       ) as phone,
       (select city
        from ak_contact
        where email = '[email protected]' and
              city is not null
        order by contact_id desc
        limit 1
       ) as city

Each column might be coming from a different row, so each gets its own query.

1
  • Thanks! it works, but it looks very scary )) In this case foreach on php will be better Mar 28, 2013 at 14:39
3

More along the lines of Gordon's answer...

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 = '[email protected]' 
        GROUP  BY email) a 
       LEFT JOIN ak_contact b 
              ON b.c_id = a.c_id 

Result

| C_ID | NAME |          EMAIL |    PHONE |   CITY |
----------------------------------------------------
| 8499 | Serj | [email protected] | 3-33-333 | London |

See the demo

6
  • 1
    I think OP needs latest email too
    – hop
    Mar 28, 2013 at 14:40
  • @hop According to the sample in the question, they're hard coding the e-mail.
    – Kermit
    Mar 28, 2013 at 14:44
  • Thanks for answer. No, email in this keys is a non formal key for the table. Someone stupid created this table :) Mar 28, 2013 at 14:44
  • @ypercube Looks like email is missing.
    – Kermit
    Mar 28, 2013 at 14:45
  • That's adding one line: fiddle-2-for-lazy-prince Mar 28, 2013 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.