我想选择结果在子查询的特定列中的位置,如何指定IN应检查的列?
Select x from foo
where x in (Select y, Max(z) as MaxEntry
From bar
Group By y)
说我选择这个:
Y Z
1 2
1 4
2 7
2 8
我想看看x是否在4或8组数据中
答案 0 :(得分:2)
如果您只检查x列,那么您可以这样写。
Select x from foo
where x in (Select x from bar where blah)
但是如果你想检查x和y那么你可以像这样写。
Select x from foo
where exists(Select * from bar where bar.x = foo.x and bar.y = foo.y )
对于您的查询,您不需要在选择
上写ySelect x from foo
where x in (Select Max(z) as MaxEntry
From bar
Group By y)
答案 1 :(得分:0)
我想你想要:
SELECT x
FROM foo
WHERE EXISTS
(SELECT 1
FROM bar
GROUP BY y
HAVING MAX(bar.z) = foo.x)
答案 2 :(得分:0)
如果我理解你的问题,那么这应该有效:
Select x from foo
join (Select y, Max(z) as MaxEntry
From bar
Group By y)
as m on m.MaxEntry = x