MySQL - 从重复项中选择

标签 mysql sql select group-by having

SELECT sample_number, date_sampled, date_completed, COUNT(*) c FROM tbl_sample GROUP BY sample_number HAVING c > 1

上面的查询为我提供了所有重复的样本编号,如下所示:-

'483662','0000-00-00 00:00:00','0000-00-00 00:00:00','2'
'493812','2011-10-14 00:00:00','2011-10-28 11:24:11','2'
'497558','1970-01-01 01:00:00','1970-01-01 01:00:00','2'

以样本编号 483662 为例:-

'483662','0000-00-00 00:00:00','0000-00-00 00:00:00'
'483662','2011-09-02 00:00:00','2011-10-04 09:39:31'

我需要扩展此查询并选择 date_completed 为“0000-00-00 00:00:00”或“1970-01-01 01:00:00”的所有重复样本编号

知道我该怎么做吗?

最佳答案

试试这个:

SELECT DISTINCT A.sample_number 
FROM tbl_sample A 
INNER JOIN (SELECT sample_number, COUNT(*) c 
            FROM tbl_sample 
            GROUP BY sample_number HAVING c > 1
           ) AS B ON A.sample_number = B.sample_number 
WHERE A.date_completed IN ('0000-00-00 00:00:00', '1970-01-01 01:00:00')

关于MySQL - 从重复项中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34287332/

相关文章:

mysql - 获取 SequelizeConnectionRefusedError : when running docker with sequelize

php - 如何从 php 数组创建一个 html 单选按钮列表?

mysql - sql 选择多个地址的订单

sql - 是否可以让 PostgreSQL 函数返回 “arbitrary” 类型?

javascript - 具有唯一选择的多选下拉列表 - React JS

php - 从两个表中选择第二个表的 LIMIT 1

mysql - 如果 start_date 和 end_date 在 start_date 和 end_date 之间,则停止在 tabe 中插入记录 php mysql

.net - 从微 Controller (带有 .Net Micro Framework 的 Arduino 或 Fez)连接到 SQL Server

mysql - SQL选择计算

html - 如何在中心而不是文本中显示 bootstrap selectpicker?