mysql 多行 连续_MySQL按连续行分组

这是另一个与MySQL变量一起使用的版本,不需要进行3级嵌套.第一个记录按postID和Date的顺序对记录进行预排序,并在每当Post ID,Type和/或操作中的一个值发生更改时,为每个组分配一个序列号.从那开始,它是一个简单的小组……不比较记录版本T与T2与T3 …如果您想要4或5个条件怎么办…您是否必须嵌套更多条目?或者只添加2个@ sql变量进行比较测试…

您的通话效率更高…

select

PreQuery.postID,

PreQuery.PostType,

PreQuery.Target,

PreQuery.Action,

PreQuery.Title,

min( PreQuery.Date ) as FirstActionDate,

max( PreQuery.Date ) as LastActionDate,

count(*) as ActionEntries,

group_concat( PreQuery.content ) as Content

from

( select

t.*,

@lastSeq := if( t.action = @lastAction

AND t.postID = @lastPostID

AND t.postType = @lastPostType, @lastSeq, @lastSeq +1 ) as ActionSeq,

@lastAction := t.action,

@lastPostID := t.postID,

@lastPostType := t.PostType

from

t,

( select @lastAction := ' ',

@lastPostID := 0,

@lastPostType := ' ',

@lastSeq := 0 ) sqlVars

order by

t.postid,

t.date ) PreQuery

group by

PreQuery.postID,

PreQuery.ActionSeq,

PreQuery.PostType,

PreQuery.Action

对于标题,您可能需要调整行…

group_concat(分别为PreQuery.Title)作为标题,

至少这将使DISTINCT标题变得简明…要让其更难,而又不通过使最大查询日期和其他元素在所有条件下获得与该最大日期相关联的一个标题来使整个查询嵌套一层以上.


版权声明:本文为weixin_35823536原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。