我有一个events
表,其中包含与这些ID相对应的ID(id
)和日期(eventDate
)(id
和eventDate
不是只有表中的列)。
SQLFiddle here。
+--------+----+---------------------+
| row_id | id | eventDate |
+--------+----+---------------------+
| 1 | 1 | 2014-02-27 23:19:41 |
| 2 | 1 | 2014-02-27 23:21:41 |
| 3 | 1 | 2014-02-27 23:21:41 |
| 4 | 2 | 2014-02-27 23:23:08 |
| 5 | 2 | 2014-02-27 23:25:08 |
| 6 | 2 | 2014-02-27 23:25:08 |
| 9 | 3 | 2014-02-28 15:36:55 |
| 8 | 3 | 2014-02-28 15:36:55 |
| 7 | 3 | 2014-02-28 15:34:55 |
| 10 | 4 | 2014-02-28 19:31:31 |
| 11 | 4 | 2014-02-28 19:33:31 |
| 12 | 4 | 2014-02-28 19:33:31 |
| 13 | 5 | 2014-02-28 19:33:34 |
| 14 | 5 | 2014-02-28 19:33:33 |
| 15 | 5 | 2014-02-28 19:31:33 |
| 16 | 6 | 2014-03-04 22:40:21 |
| 17 | 6 | 2014-03-04 22:38:21 |
| 18 | 6 | 2014-03-04 22:40:21 |
| 19 | 7 | 2014-03-04 23:08:37 |
| 20 | 7 | 2014-03-04 23:08:38 |
+--------+----+---------------------+
我想只选择表格中的那些行,其中连续事件日期对于相同的ID是相同的。
因此,我想只看到这些条目 -
+----+---------------------+
| id | eventDate |
+----+---------------------+
| 1 | 2014-02-27 23:21:41 |
| 1 | 2014-02-27 23:21:41 |
| 2 | 2014-02-27 23:25:08 |
| 2 | 2014-02-27 23:25:08 |
| 3 | 2014-02-28 15:36:55 |
| 3 | 2014-02-28 15:36:55 |
| 4 | 2014-02-28 19:33:31 |
| 4 | 2014-02-28 19:33:31 |
请注意,没有
| 6 | 2014-03-04 22:40:21 |
| 6 | 2014-03-04 22:40:21 |
在上面的结果中,因为它们不是连续的。
我知道我可以将SQL查询的输出存储在一个文件中,然后使用unix工具来执行此操作,但我想知道这是否可以通过SQL直接实现。
答案 0 :(得分:1)
虽然我的mySql有点生疏,但应该能够实现这一点。
SELECT t.*
FROM (
SELECT
id,
eventDate,
COUNT(0) AS numRows
FROM tabl
GROUP BY id, DATE(eventDate)
HAVING COUNT(0) > 1
ORDER BY eventDate
) t
如果您需要其他列,则只需将此相关子查询加回原始表。
答案 1 :(得分:0)
从your_tableName中选择id,eventDate,其中eventDate in(选择event_ate来自your_tableName group by id,eventDate with count(eventDate)> 1);
答案 2 :(得分:0)
select ta.id, ta.eventDate from
(
select row_id as ra, t1.id, t1.eventDate
from events t1
) as ta
join
(
select row_id as rb, t2.id, t2.eventDate
from events t2
) as tb
on rb = ra+1 and ta.id = tb.id and ta.eventDate = tb.eventDate
答案 3 :(得分:0)
我找到了匹配下一行的eventDate的方法,但唯一的缺点是它会返回连续日期的数量--1行。但是在你的代码中,你可以循环一次额外的时间。
SET @inc = 0;
SET @innerInc = 1;
SELECT t1.id, t1.eventDate
FROM (
SELECT id, eventDate, (@inc := @inc + 1) as increment FROM temp
) t1
WHERE t1.eventDate = (
SELECT t2.eventDate FROM (
SELECT eventDate, (@innerInc := @innerInc + 1) as increment FROM temp
) t2
WHERE t2.increment = t1.increment
);
以下是SQLFiddle:Here
答案 4 :(得分:0)
这应该能够通过单个表扫描(没有子查询,连接等)来实现。
SELECT t.id,t.eventDate
FROM (
SELECT
IF(id = @prevID AND eventDate = @prevDate, @counter, @counter := @counter+1) as c,
@prevID := id as id,
@prevDate := eventDate as eventDate
FROM events e
JOIN (SELECT @counter := 0, @prevID := NULL, @prevDate := NULL) as stuff
WHERE 1 #or some where condition for events
ORDER BY row_id ASC
) as t
GROUP BY t.c
答案 5 :(得分:-1)
如果不是特别需要位置连续输入,而是如果您按eventDate
分组并找到具有相同eventDate
的条目,那么您需要那些记录,然后是:
select *
from Table a
join (select eventDate, count(*)
from Table
group by eventDate
having count(*) > 1) b
on (a.eventDate = b.eventDate)
对数据的位置的任意依赖性表明存在一些您不共享的其他属性,并且通过该属性可以检索和排序记录。如果这样的属性决定了记录的位置,那么正是通过使用 属性进行排序或分组,您可以有效地解决这个问题。
答案 6 :(得分:-3)
抛弃我的自我加入后,我认为你将不得不为每个sub_query生成row_numbers:
select @rn1 := @rn1+1 as ra, t1.id, t1.eventDate
from events t1
join (select @rn1 := 0) r;
然后将其加入
select @rn2 := @rn2+1 as rb, t2.id, t2.eventDate
from events t2
join (SELECT @rn2 := 0) r;
所以最终答案:
select ta.id, ta.eventDate from
(
select @rn1 := @rn1+1 as ra, t1.id, t1.eventDate
from events t1
join (select @rn1 := 0) r
) as ta
join
(
select @rn2 := @rn2+1 as rb, t2.id, t2.eventDate
from events t2
join (SELECT @rn2 := 0) r
) as tb
on rb = ra+1 and ta.id = tb.id and ta.eventDate = tb.eventDate
结果:
2014年2月1日,27:23 23:21:41 + 0000
2014年2月2日,27:23 23:25:08 + 0000
2014年2月3日,28日15:36:55 + 0000
2014年2月4日,28日19:33:31 + 0000