gpt4 book ai didi

MySQL 日期比较过滤器

转载 作者:可可西里 更新时间:2023-11-01 07:54:49 25 4
gpt4 key购买 nike

我有一些运行良好的 SQL 代码,可以从我的 Wordpress 数据库返回所需的结果。

但是,我根本无法理解如何在 say 之间过滤日期:

2010-12-10 00:00:00

2010-12-15 00:00:00

这是我的 SQL 代码:

$SQL_K =  "SELECT SQL_CALC_FOUND_ROWS wp_posts.* 
FROM wp_posts
JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE 1 = 1
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
AND wp_postmeta.meta_key = 'expiry_date'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 100 ";

最佳答案

WHERE date_column BETWEEN STR_TO_DATE('2010-12-10', '%Y-%m-%d') AND STR_TO_DATE('2010-12-15', '%Y-%m-%d')

像这样吗?

编辑:忘记了结束语。哎呀

EDITv2:使用更新的查询添加您的代码

EDITv3:小优化删除了 1=1 比较,对 wp_posts.post_status 使用 IN()

SELECT      SQL_CALC_FOUND_ROWS wp_posts.* 
FROM wp_posts
JOIN wp_postmeta
ON (wp_posts.ID = wp_postmeta.post_id)
WHERE wp_posts.post_type = 'post'
AND (wp_posts.post_status IN ('publish','private'))
AND wp_postmeta.meta_key = 'expiry_date'
AND ___INSERT_NAME_OF_DATE_COLUMN_HERE___
BETWEEN STR_TO_DATE('2010-12-05', '%Y-%m-%d')
AND STR_TO_DATE('2010-12-15', '%Y-%m-%d')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 100

关于MySQL 日期比较过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4412257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com