1
votes

This may be a simple question but I can;t find the answer anywhere. Here is an abstracted version of the problem:

  • I have a table 'Data' which has a column named 'Key' and a column named 'Value'
  • I have another table 'Users' which has columns named 'ID','Key1','Key2'
  • I want some sort of join which will give columns 'ID','Value1', and 'Value2' where Value1 is the value from Key1 and Value 2 is the value from Key2.
3

3 Answers

2
votes

try

select u.id, d1.v as v1, d2.v as v2 from users u 
inner join data d1 on u.k1 = d1.k 
inner join data d2 on u.k2 = d2.k

you can check that it works on SQLFiddle

1
votes
select d1.value, d2.value from Users 
inner join Data d1 on users.key1 = d1.key 
inner join Data d2 on users.key2 = d2.key 
0
votes
SELECT
    a.ID,
    b.Value AS Value1,
    c.Value AS Value2
FROM 
    users a
INNER JOIN
    data b ON a.Key1 = b.Key
INNER JOIN
    data c ON a.Key2 = b.Key