SQL: Multiple matches towards same table?
Probably a very basic solution to this, but I can't seem to figure it out. I'm trying to match multiple ID's in one table towards another.
Structure is something like this,
tt_staff (ID - name)
2 - Lenny
3 - Carl
tt_run (producer1 - producer2)
2 (i.e Lenny)
3 (i.e Ca开发者_如何学编程rl)
I want to create a view with a single row that shows me the names of both Carl and Lenny, rather then their ID's.
Tried the following,
SELECT e.*, run.*, s.s_name FROM tt_run AS run, tt_events AS e, tt_staff AS s
WHERE e.e_ID = run.e_ID
AND run.e_bandproducer1 = s.s_ID
AND run.e_bandproducer2 = s.s_ID
This obviously doesn't work, since the ID is found on producer1. I've also tried with UNION, but not familiar enough with it (I did manage to get the right result, but in two rows).
As always, thanks for any replies.
It looks like you just need to join tt_staff twice:
SELECT e.*, r.*, s1.s_name, s2.s_name
FROM tt_events e
INNER JOIN tt_run r ON e.e_ID = r.e_ID
INNER JOIN tt_staff s1 ON r.e_bandproducer1 = s1.s_ID
INNER JOIN tt_staff s2 ON r.e_bandproducer2 = s2.s_ID
精彩评论