mysql递归查询连续时间的个数,在线等。

mysql递归查询连续时间的个数,在线等。,第1张

SELECT COUNT(*) FROM tablename WHERE (username = 'Jack') and

(DATE_ADD(sign_date, INTERVAL 1 DAY) IN

(SELECT sign_date FROM tablename WHERE (username = 'Jack'))

)

这里么有考虑5号星期五签到8号星期一签到也是连续签到的情况,抛砖引玉,你已经可以完成了。

你可以需要用到的函数:DAYOFWEEK

应该是这样:

with qry as (select user_id,parent_id from tab where user_id = 32

union all

select tab.user_id,tab.parent_id from tab,qry

where tab.parent_id = qry.id)

select * from qry

我用mysql5.0.22,不支持上述语法,oracle就可以(sqlserver应该也可以):

create table tab1(user_id int, parent_id int)

insert into tab1 values(1,null)

insert into tab1 values(32,1)

insert into tab1 values(101,32)

insert into tab1 values(102,32)

insert into tab1 values(201,101)

insert into tab1 values(202,101)

insert into tab1 values(203,102)

insert into tab1 values(204,102)

select * from tab1

with qry(user_id,parent_id) as (select user_id,parent_id from tab1 where user_id = 32

union all

select tab1.user_id,tab1.parent_id from tab1,qry

where tab1.parent_id = qry.user_id

)

select * from qry

所以,mysql没有办法了,只有写函数,用循环来实现了。

我给你举个递归查询嵌套的例子,你看一下就明白了。

select yr_student_info.name from yr_student_info where yr_student_info.school_id in (select yr_school_info.id from yr_school_info where yr_school_info.province='上海' and yr_school_info.city='上海')


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7492931.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存