在 mysql 5.0 中使用 REGEXP 比较文件

发布于 2024-11-09 02:22:07 字数 634 浏览 1 评论 0原文

我想要一个嵌套的 mysql 请求来计算每天的操作,如下所示:

SELECT `timestamp` AS `date`, 
  (SELECT COUNT('id') 
   FROM `actions` WHERE `timestamp` = `date`) AS `action_num`  
FROM `actions`;  

如果不是这样的话,那会很好,这样我就需要计算每一天,而我得到的只是每个时间戳。如果只是约会的话就可以了。所以我想如果我可以从字符串中取出日期并进行比较,这可能会起作用:

SELECT `timestamp` AS `date`, 
  (SELECT COUNT('id') FROM `actions` 
   WHERE `timestamp` REGEXP '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}' = `date`
   REGEXP '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}') AS `action_num`  
FROM `actions`;

但它不起作用。

关于如何直接在 MySQL 中比较两个时间戳字段的日期有什么想法吗?

谢谢

I would like to so a nested mysql request that is counting actions for each day like this:

SELECT `timestamp` AS `date`, 
  (SELECT COUNT('id') 
   FROM `actions` WHERE `timestamp` = `date`) AS `action_num`  
FROM `actions`;  

That would work great if it wasn't so that I need to calculate each day and all I got is each timestamp. If it was only a date it would work. So I thought if I can take out the date from the string and compare it this might work:

SELECT `timestamp` AS `date`, 
  (SELECT COUNT('id') FROM `actions` 
   WHERE `timestamp` REGEXP '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}' = `date`
   REGEXP '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}') AS `action_num`  
FROM `actions`;

But it did not work.

Any ideas on how to compare the dates from two timestamp fields directly in MySQL?

Thanks

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

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

发布评论

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

评论(2

被翻牌 2024-11-16 02:22:07

非常简单,内部查询应该是:

SELECT count(id) FROM actions 
WHERE DATE(`timestamp`) = DATE(`date`) as action_num

参见: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date

Very easy, the inner query should be:

SELECT count(id) FROM actions 
WHERE DATE(`timestamp`) = DATE(`date`) as action_num

See: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date

红玫瑰 2024-11-16 02:22:07

您可以使用DATEDIFF()函数来比较MySQL中的日期,
TIMESTAMPDIFF() 用于比较时间戳,DATE() 用于从时间戳获取日期信息。
更多日期和时间函数和示例可在 MySQL 中找到参考手册页

You can use DATEDIFF() function to compare the dates in MySQL,
and TIMESTAMPDIFF() for comparing timestamps , DATE() to get date information from timestamp.
More date and time functions and examples are available in MySQL's reference manual pages.

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