mysql 查询两个日期字段之间的日期

发布于 2025-01-02 01:23:34 字数 306 浏览 2 评论 0原文

我正在使用 MySql 5.5。

我需要找到具有特定 IP 地址的日期的用户 ID。

这些字段是用户 ID、IP 地址、开始日期、结束日期。

例如,我正在寻找 2011 年 9 月 12 日 IP 地址为 192.168.1.1 的用户 ID。

查询将类似 select * from database where ipaddress='192.168.1.1' and 2011-12-09 is in(startdate and enddate);

欢迎指出此逻辑缺陷的任何帮助。谢谢。

I'm using MySql 5.5.

I need to find a userid on a date with a particular ip address.

The fields are userid, ipaddress, startdate, enddate.

So for instance I am looking for a userid with ip address 192.168.1.1 on Sep 12 2011.

the query would be similar
select * from database where ipaddress='192.168.1.1' and 2011-12-09 is in(startdate and enddate);

Any help to pointing out this logic flaw is welcome. Thank you.

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

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

发布评论

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

评论(3

稍尽春風 2025-01-09 01:23:34

不太清楚您是否想要:

'2011-12-09' BETWEEN startdate AND enddate

或:

'2011-12-09' = startdate AND '2011-12-09' = enddate

It's not very clear if you want:

'2011-12-09' BETWEEN startdate AND enddate

or:

'2011-12-09' = startdate AND '2011-12-09' = enddate
伏妖词 2025-01-09 01:23:34

明显的解决方案是这样的:

  [...]
AND
  startdate <= '2011-12-09'
AND
  enddate >= '2011-12-09'

但是有一个快捷方式 使用 < code>BETWEEN 所以你可以简单地写:

  [...]
AND
  '2011-12-09' BETWEEN startdate AND enddate

注意: BETWEEN 也适用于数字、字符串和其他东西,并且可以否定它通过写作NOT BETWEEN - 有时非常有用。

the obvious solution would be like:

  [...]
AND
  startdate <= '2011-12-09'
AND
  enddate >= '2011-12-09'

but theres a shortcut using BETWEEN so you can simply write:

  [...]
AND
  '2011-12-09' BETWEEN startdate AND enddate

note: BETWEEN also works for numbers, strings and other stuff, and it's possible to negate it by writing NOT BETWEEN - quite useful sometimes.

过气美图社 2025-01-09 01:23:34

由于有开始和结束日期,可能是这样的:

SELECT * FROM table WHERE ipaddress = '192.168.1.1' AND '2011-12-09' >= startdate AND '2001-12-09' <= enddate;

或者正如 ypercube 所指出的,您可以使用 BETWEEN

SELECT * FROM table WHERE ipaddress = '192.168.1.1' AND '2011-12-09' BETWEEN startdate AND enddate;

Since there is a start and end date, maybe something along the line of:

SELECT * FROM table WHERE ipaddress = '192.168.1.1' AND '2011-12-09' >= startdate AND '2001-12-09' <= enddate;

Or as pointed out by ypercube you can use BETWEEN:

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