如何在SQL中找到两个特定时间之间的数据

发布于 2024-11-30 08:34:11 字数 238 浏览 1 评论 0原文

我需要运行一个查询来查找昨天凌晨 2 点到今天凌晨 2 点之间发生的所有登录日期 (dd/mm/yyyy) 和时间 (hh.mm)。数据库中的登录时间格式为mm-dd-yyyy hh.mm.ss

我尝试了多个 selectBetween 查询,这些查询要么在错误的日期返回数据,要么在时间跨度之外返回数据。

I need to run a query that finds all login dates (dd/mm/yyyy) and times (hh.mm) that occurred between 2am yesterday and 2am today. The login time in the database is formatted as mm-dd-yyyy hh.mm.ss.

I have tried multiple select and between queries that either return data on the wrong day or outside the time spans.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情深如许 2024-12-07 08:34:11

根据您提供的信息,我只能给出一个通用示例:

SELECT    *
FROM      [YourTable]
WHERE     [YourDate] BETWEEN '08-15-2011 02:00:00' AND '08-16-2011 02:00:00'

**编辑**

根据评论中的良好建议,这是一个可重复使用的版本:

SELECT    *
FROM      [YourTable]
WHERE     [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
                     AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'

Based on the information that you provided, I can only give a generic example:

SELECT    *
FROM      [YourTable]
WHERE     [YourDate] BETWEEN '08-15-2011 02:00:00' AND '08-16-2011 02:00:00'

**EDIT**

Per a good suggestion from the comments, here is a reusable version:

SELECT    *
FROM      [YourTable]
WHERE     [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
                     AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'
初与友歌 2024-12-07 08:34:11

假设 mysql:

SELECT * FROM your_table 
WHERE STR_TO_DATE(your_date, '%m-%d-%Y %H.%i.%s') BETWEEN
DATE_SUB(STR_TO_DATE(DATE_FORMAT(NOW(), '%m-%d-%Y'), '%m-%d-%Y'), INTERVAL 22 HOUR) AND
DATE_ADD(STR_TO_DATE(DATE_FORMAT(NOW(), '%m-%d-%Y'), '%m-%d-%Y'), INTERVAL 2 HOUR)

我确信有更好的方法。

Assuming mysql:

SELECT * FROM your_table 
WHERE STR_TO_DATE(your_date, '%m-%d-%Y %H.%i.%s') BETWEEN
DATE_SUB(STR_TO_DATE(DATE_FORMAT(NOW(), '%m-%d-%Y'), '%m-%d-%Y'), INTERVAL 22 HOUR) AND
DATE_ADD(STR_TO_DATE(DATE_FORMAT(NOW(), '%m-%d-%Y'), '%m-%d-%Y'), INTERVAL 2 HOUR)

I'm sure there's a better way though.

寒冷纷飞旳雪 2024-12-07 08:34:11

如果我们如下使用,查询不会返回任何记录...

SELECT    *
FROM      [YourTable]
WHERE     [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
                 AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'

我们正在使用 Between 子句,因此最旧的日期应该是第一个,查询如下...

SELECT    *
FROM      [YourTable]
WHERE     [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'
                 AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'

The query doesn't return any records if we use as follows...

SELECT    *
FROM      [YourTable]
WHERE     [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
                 AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'

We are using between clause so the oldest date should be first and the query becomes as follows...

SELECT    *
FROM      [YourTable]
WHERE     [YourDate] BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-1), 0) + '02:00'
                 AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) + '02:00'
半衬遮猫 2024-12-07 08:34:11

这也可以帮助你

where pt.status in (1) 
    and pt.is_visible in ('t') 
    and pt.merchant in (0) 
    and (date(pt.created_at+'05:30') >= current_date-2 
            and extract(hour from(pt.created_at+'05:30')) >=17)
    and ((pt.created_at+'05:30') <= current_date-1
            and extract(hour from(pt.created_at+'05:30')) <=17)

This can also help you out

where pt.status in (1) 
    and pt.is_visible in ('t') 
    and pt.merchant in (0) 
    and (date(pt.created_at+'05:30') >= current_date-2 
            and extract(hour from(pt.created_at+'05:30')) >=17)
    and ((pt.created_at+'05:30') <= current_date-1
            and extract(hour from(pt.created_at+'05:30')) <=17)
○闲身 2024-12-07 08:34:11

我认为您可以使用这些提示:

SET @start = CURDATE() - interval 1 DAY + interval 2 HOUR;
SET @end = CURDATE() + interval 2 HOUR;

并且:

SELECT * FROM TABLE_NAME WHERE DATE_FIELD BETWEEN @start AND @end

I think you can use these tips :

SET @start = CURDATE() - interval 1 DAY + interval 2 HOUR;
SET @end = CURDATE() + interval 2 HOUR;

And :

SELECT * FROM TABLE_NAME WHERE DATE_FIELD BETWEEN @start AND @end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文