MySQL:根据指定条件排除某些结果

标签 mysql

我遇到了一些问题。该问题有两个表,即黑客和挑战。 这是架构:

Hackers(hacker_id: int, name string)
Challenges(challenge_id: int, hacker_id int)

我正在尝试编写一个查询来打印 hacker_id、姓名以及每个学生创建的挑战总数。按挑战总数降序对结果进行排序。如果不止一名学生创建了相同数量的挑战,则按 hacker_id 对结果进行排序。如果超过一名学生创建了相同数量的挑战,并且计数小于创建的最大挑战数,则从结果中排除这些学生。

这是我的查询:

select hackers.hacker_id , 
       hackers.name , 
       count(challenges.challenge_id) as challenges_created 
       from 
       hackers left join  challenges 
       on 
       hackers.hacker_id = challenges.hacker_id  
       having 
       count(challenges_created) >= max(challenges_created) 
       order by challenges_created desc, hackers.hacker_id asc  

我得到了错误的输出! 请告诉我哪里错了!

示例输入:

黑客表:image 挑战者表:image

Sample Output:
21283  Angela  6
88255  Patrick  5
96196  Lisa  1  

最佳答案

这是一个仅在 mysql 中解决的疯狂需求。使用 CTE(mysql 不支持),您可以重用您的查询:

with tmp as (
    select h.hacker_id, h.name, count(1) as challenges_created
    from hackers h
    left join challenges c on c.hacker_id = h.hacker_id
    group by h.hacker_id, h.name
    order by challenges_created desc, h.hacker_id asc 
), max_challenges_created as (
    select max(challenges_created) as challenges_created
    from tmp
), count_per_challenges_created as (
    select challenges_created, count(1) as c
    from tmp
    group by challenges_created
)
select *
from tmp
cross join max_challenges_created m
left join count_per_challenges_created c on c.challenges_created = tmp.challenges_created
where tmp.challenges_created = m.challenges_created
   or c.c = 1

http://rextester.com/XGYQ11641

在 MySQL 中,您甚至无法使用临时表,因为您无法在一个查询中多次使用它。所以你必须复制并粘贴相同的查询三次(并希望mysql使用缓存只执行一次):

select *
from (
    select h.*, count(1) as challenges_created
    from hackers h
    left join challenges c on c.hacker_id = h.hacker_id
    group by h.hacker_id
    order by challenges_created desc, h.hacker_id 
) tmp
cross join (
    select max(challenges_created) as challenges_created
    from (
        select h.*, count(1) as challenges_created
        from hackers h
        left join challenges c on c.hacker_id = h.hacker_id
        group by h.hacker_id
        order by challenges_created desc, h.hacker_id asc
    ) tmp
) m
left join (
    select challenges_created, count(1) as c
    from (
        select h.*, count(1) as challenges_created
        from hackers h
        left join challenges c on c.hacker_id = h.hacker_id
        group by h.hacker_id
        order by challenges_created desc, h.hacker_id asc
    ) tmp
    group by challenges_created
) c on c.challenges_created = tmp.challenges_created
where tmp.challenges_created = m.challenges_created
   or c.c = 1

http://rextester.com/IDUMRB49795

关于MySQL:根据指定条件排除某些结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37757355/

相关文章:

Mysql使用系统文件缓存,而不是直接使用RAM

mysql - 根据输入参数计数检索许多结果

MySQL 工作台 : Cannot export a database

java - Java 中的 MySQL 连接器

php - 使用哈希密码登录 php

mysql - 即使值为零,条形图也会出现 - Google Charts

mysql - SolR DIH 文件串联

mysql - golang gorm 访问底层mysql查询

php - 获取mysqldump调试信息

php - 使数据库操作和发送电子邮件成为交易