请帮助我加入命令的结果(MS SQL):
SELECT name,value FROM table1 WHERE idfoo1 IN(SELECT _id FROM table3 where id = 1);
SELECT value FROM table2 WHERE idfoo2 IN(SELECT _id_2 FROM table3 where id = 1) AND name='fooname';
我得到了:
name value
John 2
Bill 32
Alex 11
value
434
234
144
但我需要加入结果。
name value value
John 2 434
Bill 32 234
Alex 11 144
所以,id == id,_id!= _id_2,
答案 0 :(得分:3)
使用此查询:
SELECT t1.name,
t1.value,
t2.value
FROM table1 t1
INNER JOIN table3 t3 ON t1.idfoo1 = t3._id
INNER JOIN table2 t2 ON t2.idfoo2 = t3._id_2
WHERE t3.id=1 AND t2.name = 'fooname'
答案 1 :(得分:1)
Select a.name,a.value,c.value FROM table1 as a inner join table3 as b
on a.idfoo1=b.id and b.id=1 inner join table3 as c
on c.idfoo2=b._id_2 and b.id=1 and c.name='fooname'
答案 2 :(得分:-2)
我想这就是你所需要的 -
SELECT t1.name, t1.value, t2.value
FROM table1 t1, table2 t2, table3 t3
WHERE
t1.idfoo1 = t3._id
AND t2.idfoo2 = t3._id_2
AND t3.id = 1
AND t2.name='fooname';