0

I have a Profiles table, a Workers table and a Studios table.

My profiles table looks like this:

mysql> select * from Profiles;
    | profile_id | owner_luser_id | profile_type | profile_name | profile_entity_name | description | profile_image_url | search_rating |

my Studios and Workers tables basically have extended data about the entity, depending on what profile_type (ENUM) is set to [Worker, Studio].

I need to know how I can do a single SQL query to retrieve (all) the profile data from Profiles, but only join on to the end of the result, specific fields from the Studios or Workers table depending on if that entity is a Studio or a Worker.

I've tried UNION with no success (UNION requires all tables to have the same number of fields?, which seems to not be what I'm looking for).

I've tried JOIN with no success also; it just shows Workers if I join on Profiles.profile_type = 'Worker'.

I just want to be able to add additional columns to the result, from either the Worker or Studio tables, depending on what the Profiles.profile_type is.

SQLFiddle of my schema is at http://sqlfiddle.com/#!2/3f599/1 - I would like to (for example) SELECT * FROM Profiles, but depending on what profile_type is, join on the end of that result either the Worker or Studio's licence number (Workers.license_number, Studios.premises_license_number respectively), and call that column for example 'profilelicense_number'.

4
  • 3
    Consider providing proper DDLs (and/or an sqlfiddle) TOGETHER WITH THE DESIRED RESULT SET
    – Strawberry
    Feb 23, 2014 at 14:29
  • what is Workers and Studios table?
    – Wasim
    Feb 23, 2014 at 14:33
  • Updated post and provided sql fiddle! thank you :) Feb 23, 2014 at 14:36
  • @Anthony, did you read my answer?
    – Wasim
    Feb 23, 2014 at 14:48

3 Answers 3

1

I think this is what you want:

SELECT p.*,
       case
         when w.license_number is not null then
          w.license_number
         when s.premises_license_number is not null then
          s.premises_license_number
         else
          null
       end as profilelicense_number
  FROM Profiles p
  LEFT JOIN Studios s
    on p.profile_id = s.profile_id
  LEFT JOIN Workers w
    on p.profile_id = w.profile_id
1

You want to use an outer join, this will join the data if they exist, and join with an empty row if they don't exist

SELECT * FROM Profiles p
    LEFT JOIN Studios s on p.profile_id = s.profile_id
    LEFT JOIN Workers w on p.profile_id = w.profile_id

alternatively if you want all profiles data but not all studios and workers data you can use p.*followed by the specific columns from Studios and Workers, for instance:

SELECT p.*, s.phone, w.phone FROM Profiles p
    LEFT JOIN Studios s on p.profile_id = s.profile_id
    LEFT JOIN Workers w on p.profile_id = w.profile_id
5
  • Using select * is in most situations undesirable - unless you actually want every column for that call you are wasting memory. Feb 23, 2014 at 14:41
  • of course but he didn't specify which columns he needs
    – peter
    Feb 23, 2014 at 14:42
  • yes - but if I was answering (which I'm not since you beat me by a few seconds) I'd mention that so that there aren't answering implying that that is a good standard Feb 23, 2014 at 14:43
  • FANTASTIC, that is exactly what I needed :) and yes, I definitely won't be using *, it was just an example to convey the idea! perfect! Feb 23, 2014 at 14:46
  • @AnthonyMusgrove if you want a single column to show one field, if the S join was used, and a DIFFERENT field if the W join was used, you have to do that with a CASE statement (see my response). Feb 23, 2014 at 14:47
0

You could use a case statement, to consolidate the extra data into one column like follows.

SELECT p.profile_id,
case p.profile_type
  when 'Studio' then s.premises_license_number
  when 'Worker' then w.license_number
  else ''
  end as profilelicense_number
from Profiles p
left outer join studios s on p.profile_id = s.profile_id
left outer join workers w on p.profile_id = w.profile_id

This doesn't account for the case of there being both a s.premises_license_number and a w.license_number, it will only show the first one found.

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.