zoukankan      html  css  js  c++  java
  • Mysql的两种“超过多少次”写法(力扣596)

    题目:

    有一个courses 表 ,有: student (学生) 和 class (课程)。

    请列出所有超过或等于5名学生的课。

    例如,表:

    +---------+------------+
    | student | class      |
    +---------+------------+
    | A       | Math       |
    | B       | English    |
    | C       | Math       |
    | D       | Biology    |
    | E       | Math       |
    | F       | Computer   |
    | G       | Math       |
    | H       | Math       |
    | I       | Math       |
    +---------+------------+
    

    应该输出:

    +---------+
    | class   |
    +---------+
    | Math    |
    +---------+
    

    Note:
    学生在每个课中不应被重复计算。

    答案1:

    select a.class from
    (select count(distinct student) as num, class from courses group by class) a
    where a.num >= 5

    答案2:
    select class from courses group by class having count(class) > 4

    对比一下效率是一样的:

    [SQL] SELECT age from test_user GROUP BY age having count(age)>1;
    受影响的行: 0
    时间: 0.001s

    [SQL]

    select a.age from
    (select count(age) as num, age from test_user group by age) a
    where a.num >= 2;
    受影响的行: 0
    时间: 0.001s

  • 相关阅读:
    redis线程模型
    同步容器和并发容器
    200+面试题
    redis pipeline
    redis事务和脚本
    redis事务
    redis优缺点
    redis持久化策略
    Redis为什么要把所有数据放到内存中?
    redis的过期策略以及内存淘汰机制
  • 原文地址:https://www.cnblogs.com/starcrm/p/10677222.html
Copyright © 2011-2022 走看看