我需要获取具有不同日期字段的记录
表网站:
field id
reference
created
我们每天都会添加大量记录,因此我需要执行一项功能,提取所有已添加的行重复的记录,以进行一些通知。
我无法得到的条件是当天的记录与表格中的旧数据之间的差异应该是(一天到4天)。
如果没有使用事务,是否有任何简单的查询。
答案 0 :(得分:0)
我不确定我是否完全明白重复记录的含义,但这是一个基本的日期查询:
SELECT fieldId, reference, created, DATE(created) as the_date
FROM Sites
WHERE the_date
BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 3 DAY ) )
AND DATE ( NOW() )
答案 1 :(得分:0)
我做了几个假设,例如:
所以,我的代码是:
with originals as (
select s1.*
from sites as s1
where 0 = (
select count(*)
from sites as s2
where s1.field_id = s2.field_id
and s1.reference = s2.reference
and s1.created <> s2.created
and DATEDIFF(DAY,s2.created, s1.created) between 1 and 4
)
)
select s1.*
from sites as s1
inner join originals as o
on s1.field_id = o.field_id
and s1.reference = o.reference
and s1.created <> o.created
where DATEDIFF(DAY,o.created, s1.created) between 1 and 4
order by 1,2,3;
这是一个小提琴:http://sqlfiddle.com/#!3/9b407/20
如果放宽某些条件,这可能会更简单。
答案 2 :(得分:0)
非常感谢每一位试图帮助我的人,
我经过多次测试后找到了这个解决方案
SELECT `id`,`reference`,count(`config_id`) as c,`created` FROM `sites`
where datediff(date(current_date()),date(`created`)) < 4
group by `reference`
having c > 1
非常感谢你的帮助