我有类似以下的MySQL:
Events
+----+------+--------------------------------+
| id | name | sites_id | created |
+----+------+--------------------------------+
| 1 | test | 1 | 2013-11-01 00:00:00 |
| 2 | test | 1 | 2013-11-02 00:00:00 |
| 3 | test | 2 | 2013-11-13 00:00:00 |
| 4 | test | 3 | 2013-11-14 00:00:00 |
| 5 | test | 4 | 2013-11-25 00:00:00 |
+----+------+----------+---------------------+
我想要选择在48小时内创建并具有相同网站ID的事件。 (在这个例子中,我希望ID 1和2)。
任何帮助都会受到赞赏,因为我已经在SQL中单独绘制了如何做到这一点。
由于
答案 0 :(得分:5)
尝试这样的事情:
SELECT DISTINCT e1.*
FROM events e1
INNER JOIN events e2 ON e1.sites_id = e2.sites_id
AND e1.id <> e2.id
WHERE ABS(datediff(e1.created, e2.created)) <= 2;
这会给你结果:
ID NAME SITES_ID CREATED
2 test 1 November, 02 2013 00:00:00+0000
1 test 1 November, 01 2013 00:00:00+0000
答案 1 :(得分:3)
我没有对此进行语法检查(因此将其视为更多指南):
SELECT DISTINCT
t1.id
, t1.name
, t1.sites_id
, t1.created
FROM
Events t1
INNER JOIN Events t2
ON (t2.created BETWEEN DATE_ADD(t1.created, INTERVAL -2 DAY) AND DATEADD(DAY, INTERVAL 2 DAY))
AND t1.sites_id = t2.sites_id
AND t1.id <> t2.id
答案 2 :(得分:0)
你去了:
SELECT * FROM table WHERE table.created > DATE_SUB(table.created, INTERVAL 48 HOUR) GROUP BY sites_id HAVING count(*) > 1;
答案 3 :(得分:0)
你可以这样做:
Select m.id,m.created from TPS_info m, tps_info c
where c.created between m.created AND date_add(m.created, INTERVAL 43200 MINUTE)
group by m.id,m.sites_id