I have the following schema:
user -- user_id | first_name
full_time_job -- ft_job_id | user_id
part_time_job -- pt_job_id | user_id
internship -- internship_id | user_id
I’d like to write a query that returns all users that exist in any of the three job tables. What’s the best way to do this? I’d prefer to use LEFT JOINS over WHERE EXISTS if possible, but will be happy with the best working solution.
I do NOT want to return any users that exist in NONE of the three job tables.
Using
LEFT JOINs, you merely need to verify thatuser_idisNOT NULLin at least one of the tables:Best to fix the schema if possible…
In the long run though, if possible these should all probably be combined into a single table that has a column indicating the job type, rather than 3 similar tables.
Sample: http://sqlfiddle.com/#!2/2cd2c/1
In the above example, users
5,6don’t exist in any of the 3 tables.