关联查询:select where条件不是max(内部查询中的条件)
我试图选择 userName 和 groupId 重复的所有行,并且 userId 不是该 userName/groupId 组合的最大 userId。到目前为止,这是我的代码:
select *
from userTable u
where exists
(select *
from userTable u1
where userName <> '' and userName is not null
and u.userName = u1.userName and u.groupId = u1.groupId
and u.userId <> max(u1.userId)
group by userName, groupId
having count(*) > 1)
order by userName
但是,该行:
and u.userId <> u1.max(userId)
给了我一个错误。
执行此查询的正确方法是什么?
I am trying to select all the rows where the userName and groupId is duplicated, and the userId is not the max userId for that userName/groupId combination. Here is my code so far:
select *
from userTable u
where exists
(select *
from userTable u1
where userName <> '' and userName is not null
and u.userName = u1.userName and u.groupId = u1.groupId
and u.userId <> max(u1.userId)
group by userName, groupId
having count(*) > 1)
order by userName
However, the line:
and u.userId <> u1.max(userId)
is giving me an error.
What is the right way to do this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这应该可以做到:
This should do it, I think: