即使数据库中有发布日期和到期日期,以下查询也不会返回任何结果
SELECT wposts.*
FROM qit_posts wposts INNER JOIN qit_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
WHERE wposts.post_type IN ('ad_listing') AND
wpostmeta.meta_key ='cp_publish_date'
AND wpostmeta.meta_value <= '2013-12-10'
AND wpostmeta.meta_key ='cp_expiry_date'
AND wpostmeta.meta_value >= '2013-12-10'
在数据库表中有一个帖子,其中有一个元字段cp_publish_date,其值为2013-12-08和cp_expiry_date 2013-12-31 查询应返回特定原始,但它不会返回它,有没有人知道这个,什么,可能是问题,这里比较日期格式没有问题,我正在处理它
答案 0 :(得分:0)
正如我所见,你在wpostmeta.meta_key中查找,但已经使用“AND”参数ur ar使用它有两个不同的值。
wpostmeta.meta_key ='cp_publish_date' AND AND wpostmeta.meta_key ='cp_expiry_date'
你应该使用“或”参数。
我想你想这样做;
SELECT wposts.*
FROM qit_posts wposts INNER JOIN qit_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
WHERE wposts.post_type IN ('ad_listing') AND (
wpostmeta.meta_key ='cp_publish_date'
AND wpostmeta.meta_value <= '2013-12-10' ) OR
(wpostmeta.meta_key ='cp_expiry_date'
AND wpostmeta.meta_value >= '2013-12-10' )
放轻松
答案 1 :(得分:0)
尝试使用STR_TO_DATE(str,format)
wp的meta_value
类型文本,您需要根据所需的日期格式将其转换为日期,还需要加入qit_postmeta
两次才能两个不同的键
SELECT wposts.*
FROM qit_posts wposts
INNER JOIN qit_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
INNER JOIN qit_postmeta wpostmeta2 ON wposts.ID = wpostmeta.post_id
WHERE wposts.post_type IN ('ad_listing')
AND wpostmeta.meta_key ='cp_publish_date'
AND STR_TO_DATE(wpostmeta.meta_value,'%d %M,%Y') <= '2013-12-10'
AND wpostmeta2.meta_key ='cp_expiry_date'
AND STR_TO_DATE(wpostmeta2.meta_value,'%d %Y,%Y') >= '2013-12-10'
GROUP BY wposts.ID
答案 2 :(得分:0)
使用OR
无济于事。试试这个:
SELECT wposts.*
FROM qit_posts wposts
JOIN (
SELECT post_id
FROM qit_postmeta
GROUP BY post_id
HAVING SUM(
CASE
WHEN meta_key ='cp_publish_date' AND meta_value <= '2013-12-10' THEN 1
WHEN meta_key ='cp_expiry_date' AND meta_value >= '2013-12-10' THEN 1
ELSE 0
END
) = 2
) wpostmeta
ON wposts.ID = wpostmeta.post_id
WHERE wposts.post_type IN ('ad_listing')