MySQL - Select 来自重复的地方
MySQL - Select from duplicates where
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'
我需要扩展此查询和 select 所有重复的样本编号,其中 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')
SELECT sample_number, COUNT(1) AS c
FROM tbl_sample
WHERE sample_number IS NOT NULL
AND (date_completed='0000-00-00 00:00:00' OR date_completed='1970-01-01 01:00:00')
GROUP BY sample_number
HAVING COUNT(1) > 1
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'
我需要扩展此查询和 select 所有重复的样本编号,其中 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')
SELECT sample_number, COUNT(1) AS c
FROM tbl_sample
WHERE sample_number IS NOT NULL
AND (date_completed='0000-00-00 00:00:00' OR date_completed='1970-01-01 01:00:00')
GROUP BY sample_number
HAVING COUNT(1) > 1