相关查询:选择条件不是最大的情况(内部查询中的条件) [英] Correlated query: select where condition not max(condition in inner query)
本文介绍了相关查询:选择条件不是最大的情况(内部查询中的条件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图选择所有重复的行,其中userName和groupId重复,并且userId不是该userName / groupId组合的最大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
但是,该行:
and u.userId <> u1.max(userId)
给我一个错误。
执行此查询的正确方法是什么?
What is the right way to do this query?
推荐答案
SELECT u.*
FROM (
SELECT userName, groupId, MAX(userId) AS maxId
FROM userTable
GROUP BY
userName, groupId
HAVING COUNT(*) > 1
) q
JOIN userTable u
ON u.userName = q.userName
AND u.groupId = q.groupId
AND u.userId <> q.maxId
这篇关于相关查询:选择条件不是最大的情况(内部查询中的条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文