RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 892213
Accepted
Oleg
Oleg
Asked:2020-10-12 19:33:32 +0000 UTC2020-10-12 19:33:32 +0000 UTC 2020-10-12 19:33:32 +0000 UTC

获取具有相同 f_key 的条目

  • 772

有一张桌子tbl:

id | f_key | alive
-------------------
   |       |

还有更多的领域,但它们并不重要。

我想知道我是否有多个当前“活动”记录 (alive = true) 引用相同的“f_key”。

我自己能做什么:
我可以写一个简单的查询

select * from tbl where alive = true order by f_key;

并亲眼看到:如果连续两行或多行具有相同的f_key,那么这就是我要找的。

但我想以某种方式自动化这项工作。我知道这个问题很简单,你只需要在 condition 的某个地方添加一个聚合函数count(),它的值不止一个,但我很困惑在哪里。

sql
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Владимир Клыков
    2020-10-12T19:36:41Z2020-10-12T19:36:41Z

    如果我理解正确,那么此查询将返回金额:

    select count(*) from tbl where alive = true group by f_key;
    

    如果您想确切知道哪些键是重复的,您可以f_key在输出中添加:

    select count(*),f_key from tbl where alive = true group by f_key;
    

    如果您需要完整的记录,那么这里有一个查询:

    select 
      * 
    from 
      tbl t1
    where (
            Select 
              count(f_key) 
            from tbl t2 
            where 
              (alive = true) and // Эта строка если дубли нужны только среди "живых"
              t2.f_key=t1.f_key
          )>1
      and (alive = true)
    
    • 4
  2. Akina
    2020-10-12T20:13:45Z2020-10-12T20:13:45Z

    获取所有记录:

    with cte as ( select id, f_key, alive, sum(alive='alive') over (partition by f_key) cnt 
                  from tbl )
    select id, f_key, alive 
    from cte 
    where cnt > 1;
    

    只获取现场录音:

    with cte as ( select id, f_key, alive, count(*) over (partition by f_key) cnt 
                  from tbl 
                  where alive='alive' )
    select id, f_key, alive 
    from cte 
    where cnt > 1;
    

    方言:MySQL 8+。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5