gpt4 book ai didi

mysql - 如何在一个字段中获取最大时间和最小时间?

转载 作者:行者123 更新时间:2023-11-29 03:01:02 25 4
gpt4 key购买 nike

我想问一下mysql查询

我有这样的表格数据

我的数据

`userid | time| date
609 | 07:56:31|2014-01-23
609 | 21:20:47|2014-01-23
609 | 17:25:27|2014-01-24
609 | 17:25:29|2014-01-24
609 | 17:13:54|2014-01-27
609 | 17:13:56|2014-01-27
609 | 07:55:09|2014-01-27`

我试过各种方法

还没有成功

喜欢

<code>set @start='1900-01-01';
set @finish='2033-12-31';
SELECT data.userid,t.clock_out,data.date from data
inner join (select max(time)as clock_out,date,userid from data where time>='10:01' group by data.userid,tgl)t
on(data.date = t.date and data.userid=t.userid and t.clock_out=date.time)</code>
规则
clock_in where time<='10:00' and clock_out time>'10:00',clock_in or clock_out can fill in the blank/empty,,the date for example 25 and 26 is not input data,but can be retrieved from variable @start='1900-01-01'and @finish='2033-12-31';
结果将是
<code>userid|date |clock_in | clock_out
609 |2014-01-23 |07:56 |21:20
609 |2014-01-24 | |17:25
609 |2014-01-25 | |
609 |2014-01-26 | |
609 |2014-01-27 |07:55 |17:13</code><p></p>

.

有人可以帮助我....:( 提前致谢....

最佳答案

我认为这就是您想要的,它与您的答案不完全相同,但我认为这是正确的。

您需要加入该用户 ID 的所有唯一日期的列表,然后使用该列表进行分组并从时间中获取 min() 和 max()。

select t1.userid, 
t2.date,
min(t1.time) as in_time,
max(t1.time) as out_time
from test t1
join (select distinct date from test where userid = 609) t2
where t1.date = t2.date
and userid = 609
group by t1.userid, t2.date;

我还要说的是,如果不存储日期和时间,只存储日期时间,然后按选择日期部分的某个函数进行分组,这似乎要好得多。

http://sqlfiddle.com/#!2/7b8732/1

关于mysql - 如何在一个字段中获取最大时间和最小时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23596128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com