SQL 只返回值相同的行

问题描述 投票:0回答:1

我正在努力获取 SQL 中值相同的行。

例子

recnr test_id
1 101
2 102
3 102
4 103
5 104
6 104
7 104
8 105

我想多次查看所有具有相同 test_id 编号的记录。所以从上面的示例中我想看到:

recnr test_id
2 102
3 102
5 104
6 104
7 104

有人知道如何实现吗?

提前致谢。

我确实尝试过使用 group by 和 stuf 但可能是错误的方式。

group-by having
1个回答
0
投票

我们首先使用

test_id
group by
得到
having count(1) > 1
的列表然后使用
inner join
我们得到预期的数据:

select *
from mytable t
inner join (
  select test_id
  from mytable
  group by test_id
  having count(1) > 1
) as s on s.test_id = t.test_id

这是一个在 mysql 上测试过的工作解决方案 ..在这里演示

© www.soinside.com 2019 - 2024. All rights reserved.