2

我需要找到在我的数据库中获得奖金的用户。唯一让我感兴趣的用户是那些获得超过一次奖金的用户。

我应该如何处理此查询以仅获得多次获得奖金的用户?

select Bonus, BonusUser, BonusType, Amount 
from Bonus 
where BonusType="1" 
order by BonusUser asc;

我需要一个提示所有“重复”行的查询,这样我就可以从中删除奖金。

我之前没有解释过,但是一些用户利用了一个错误并且可以获得免费奖金,所以我必须选择那些重复的行,分析并删除它是否是滥用案例。

4

3 回答 3

3

你可以像下面那样做

   select BonusUser,
          count(*)
   from Bonus 
  where BonusType="1" 
  group by BonusUser
   having count(*)>1
  order by BonusUser asc

您必须提供一些具有预期结果的虚拟数据..

于 2012-04-19T16:41:19.613 回答
2

作为其他答案的替代方法,您还可以执行以下操作:

SELECT DISTINCT b1.*
FROM Bonus b1
JOIN Bonus b2
  ON b1.BonusUser = b2.BonusUser
  AND b1.Id > b2.Id
WHERE b1.BonusType = "1"
  AND b2.BonusType = "1"
ORDER BY b1.BonusUser ASC;
于 2012-04-19T16:44:56.617 回答
2

添加一个GROUP BY和一个HAVING子句

SELECT Bonus
    , BonusUser
    , BonusType
    , Ammount 
FROM Bonus 
WHERE BonusType="1" 
GROUP BY BonusUser
HAVING Count(*) > 1
ORDER BY BonusUser asc;

根据您的评论,我认为这就是您想要的,这将为您提供所有有奖金的用户列表,但它会给您提供超过一项奖金的用户数量:

SELECT Bonus
    , t.BonusUser
    , BonusType
    , amount 
    , t2.cntbonus
FROM Bonus t
inner join 
(
    select count(*) as CntBonus, bonususer
    from Bonus
    where BonusType='1'
    group by bonususer
) t2
    on t.bonususer = t2.bonususer
WHERE BonusType='1'
ORDER BY BonusUser asc
于 2012-04-19T16:40:27.850 回答