MYSQL获取一定时间范围内的数据

发布于 2024-10-18 13:43:48 字数 349 浏览 6 评论 0原文

嘿大家, 无法正确获取此 MYSQL 查询,

SELECT * 
FROM tbl_problem 
WHERE user_id = 1
AND problem_solved != -1 
AND problem_timestamp BETWEEN '20110212' AND DATE('20110212', INTERVAL 14 DAY) 
ORDER BY problem_id

问题是 20110212 是由 php 动态生成的,这就是我使用 DATE/INTERVAL COMBO 的原因。

我想做的是在两周时间范围内选择条目,因为您知道这两周周期的开始日期。

提前致谢

hey everyone,
having problem getting this MYSQL query correct

SELECT * 
FROM tbl_problem 
WHERE user_id = 1
AND problem_solved != -1 
AND problem_timestamp BETWEEN '20110212' AND DATE('20110212', INTERVAL 14 DAY) 
ORDER BY problem_id

the problem is 20110212 is dynamically generated by php, which is why i use the DATE/INTERVAL COMBO.

what i am trying to do is select entries within a 2 week time frame given that you know the start date of that two week cycle.

thanks in advance

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

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

发布评论

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

评论(2

望喜 2024-10-25 13:43:48

为什么不直接让 PHP 重新格式化它,然后查询 要将

SELECT * 
FROM tbl_problem 
WHERE user_id = 1
AND problem_solved != -1 
problem_timestamp > '2011-01-24' AND problem_timestamp < DATE_ADD('2011-02-12', INTERVAL 14 DAY) 
ORDER BY problem_id

PHP 中的日期从当前格式转换为 MYSQL 格式,请使用

$date = date_create_from_format('Ymd', '20110212');
$mysqlDate = date_format($date, 'Y-m-d'); //2011-01-24

Why not just get PHP to reformat it and then query for

SELECT * 
FROM tbl_problem 
WHERE user_id = 1
AND problem_solved != -1 
problem_timestamp > '2011-01-24' AND problem_timestamp < DATE_ADD('2011-02-12', INTERVAL 14 DAY) 
ORDER BY problem_id

To convert the date in PHP from your current format to MYSQL's format, use

$date = date_create_from_format('Ymd', '20110212');
$mysqlDate = date_format($date, 'Y-m-d'); //2011-01-24
吹梦到西洲 2024-10-25 13:43:48

使用日期差异。

SELECT * 
FROM tbl_problem 
WHERE user_id = 1
AND problem_solved != -1 
AND DATEDIFF(problem_timestamp,'20110112') <= 14
ORDER BY problem_id

Use Datediff.

SELECT * 
FROM tbl_problem 
WHERE user_id = 1
AND problem_solved != -1 
AND DATEDIFF(problem_timestamp,'20110112') <= 14
ORDER BY problem_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文