mysql 查询日期时间字段的准确性

发布于 2025-01-03 20:41:53 字数 753 浏览 1 评论 0原文

我正在使用 MySql 5.5,

我在半径会计表上使用以下查询。

  • 查询1:

    SELECT * FROM database.table WHERE framedipaddress='192.168.1.1' 和 '2011-09-09' BETWEEN acctstarttime 和 acctstoptime;

acctstarttimeacctstoptime 字段为 datetime 类型,格式为 YEAR-MT-DY HR:MN:SC

使用 query2 显示我的 query1 的结果并不总是准确的,query1 有时不会返回任何内容或返回不正确的行。

  • 查询2:

    SELECT * FROM database.table WHERE framedipaddress = '192.168.1.1' ORDER BY acctstarttime DESC LIMIT 0, 400;

是否有不同的方法来查询两个日期时间字段,或者数据库健全性/数据是问题吗?

欢迎对我的逻辑错误提出任何指导。

这是我现在查询的问题

I'm using MySql 5.5,

I am using the following query on a radius accounting table.

  • query1:

    SELECT * FROM database.table WHERE framedipaddress='192.168.1.1' and '2011-09-09' BETWEEN acctstarttime AND acctstoptime;

The acctstarttime and acctstoptime fields are type datetime, formatted as YEAR-MT-DY HR:MN:SC.

Using query2 reveals my results of query1 are not always accurate, query1 sometimes will return nothing or an incorrect row.

  • query2:

    SELECT * FROM database.table WHERE framedipaddress = '192.168.1.1' ORDER BY acctstarttime DESC LIMIT 0, 400;

Is there a different method to query two datetime, fields, or is it the database sanity/data is the issue?

Any guidance to the errors in my logic are welcome.

Here is the question for the query I have now.

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

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

发布评论

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

评论(1

起风了 2025-01-10 20:41:53

'2011-09-09' = '2011-09-09 00:00:00' 因此,您仅查询在此时间之前具有 acctstarttime 且在该时间之后具有 acctstoptime 的帐户。

如果您正在查找于 2011 年 9 月 9 日开始的帐户。 那么您可以这样做

...
WHERE
acctstarttime >= '2011-09-09' AND acctstarttime < '2011-09-10'

如果您正在查找于 2011 年 9 月 9 日开始和结束的帐户, 。然后执行以下操作:

...
WHERE
acctstarttime >= '2011-09-09' AND acctstarttime < '2011-09-10' AND
acctstoptime >= '2011-09-09' AND acctstoptime < '2011-09-10' AND

仅供参考,如果您使用 BETWEEN,它会包含边界,因此它将包含 2011-09-10 00:00:00

'2011-09-09' = '2011-09-09 00:00:00' So you are only querying accounts that had an acctstarttime before this time AND an acctstoptime after this time.

If you're looking for accounts that were started on 2011-09-09. Then you could do

...
WHERE
acctstarttime >= '2011-09-09' AND acctstarttime < '2011-09-10'

If you're looking for accounts that were started and ended on 2011-09-09. Then do:

...
WHERE
acctstarttime >= '2011-09-09' AND acctstarttime < '2011-09-10' AND
acctstoptime >= '2011-09-09' AND acctstoptime < '2011-09-10' AND

FYI if you use BETWEEN it includes the bounds so it would include 2011-09-10 00:00:00.

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