Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple columns from SELECT nested within CASE MySQL

Tags:

sql

mysql

case

Is there a better way to do this?

SELECT subs. * ,
       CASE subs.member_type
         WHEN 'member' THEN 
         ( SELECT CONCAT_WS( ' ', members.first_name, members.last_name )
             FROM members
            WHERE members.id = subs.member_id)
         ELSE 
         ( SELECT members_anon.username
             FROM members_anon
            WHERE members_anon.id = subs.member_id)
       END AS fullname,
       CASE subs.member_type
         WHEN 'member' THEN 
         ( SELECT members.email
             FROM members
            WHERE members.id = subs.member_id)
         ELSE 
         ( SELECT members_anon.email
             FROM members_anon
            WHERE members_anon.id = subs.member_id)
       END AS email
  FROM subs
 WHERE subs.item_id =19
   AND subs.item_type = 'blog'
 LIMIT 0 , 30

Ideally I would like to have only one CASE section that returned name and email from the relevant table.

like image 490
Michael Robinson Avatar asked Aug 29 '10 22:08

Michael Robinson


2 Answers

I would use left outer joins on both tables:

SELECT subs. * ,
       CASE subs.member_type
         WHEN 'member' THEN CONCAT_WS( ' ', m.first_name, m.last_name )
         ELSE ma.username
       END AS fullname,
       CASE subs.member_type
         WHEN 'member' THEN m.email
         ELSE ma.email
       END AS email
  FROM subs
  LEFT OUTER JOIN members m on (m.id = subs.member_id)
  LEFT OUTER JOIN members_anon ma on (ma.id = subs.member_id)
 WHERE subs.item_id =19
   AND subs.item_type = 'blog'
 LIMIT 0 , 30

Regarding the only one case wish, if you need two different columns on your resultset, you will need two case sentences.

like image 113
Pablo Santa Cruz Avatar answered Nov 03 '22 21:11

Pablo Santa Cruz


You can't use a single case expression to handle two separate columns...

Use:

   SELECT s. *,
          CASE s.member_type
            WHEN 'member' THEN x.fullname
            ELSE y.fullname
          END AS fullname,
          CASE subs.member_type
            WHEN 'member' THEN x.email
            ELSE y.email
          END AS email
     FROM SUBS s
LEFT JOIN (SELECT m.id,
                  CONCAT_WS( ' ', members.first_name, members.last_name ) AS fullname,
                  m.email
             FROM MEMBERS m) x ON x.id = s.member_id
LEFT JOIN (SELECT ma.id,
                  ma.username,
                  ma.email
             FROM MEMBERS_ANON ma) y ON y.id = s.member_id                         
    WHERE s.item_id = 19
      AND s.item_type = 'blog'
    LIMIT 0 , 30
like image 42
OMG Ponies Avatar answered Nov 03 '22 19:11

OMG Ponies