tags:

views:

39

answers:

2

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.

+2  A: 

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.

Pablo Santa Cruz
Thanks for your answer, it works well. I'm not sure I totally get it though - are the `LEFT OUTER` joins only made if the comparison within their respective brackets evaluates `true`?
Michael Robinson
Aha! I think I get it now. The case 'adds' selections to the SELECT portion, so if `subs.member_type = 'member'` then the SELECT bit will be: `subs.*, m.username, m.email`. So the `LEFT OUTER` on ma will be executed but not included in the result set. Right? I suppose this won't have any performance impact as all `*.id` columns are `primary_keys`?
Michael Robinson
+1  A: 

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
OMG Ponies