我正在为WordPress编写一个SQL查询,用于搜索用户搜索之间的实时事件。我现在正在对其进行硬编码以进行测试。
目前,每次post_id出现在wp_postmeta表中时,都会返回两个事件,任何人都可以看到这个逻辑可能存在问题吗?
SELECT ID, post_title
FROM $wpdb->posts AS post
INNER JOIN $wpdb->postmeta AS meta
ON (post.ID = meta.post_id)
INNER JOIN $wpdb->postmeta AS meta2
ON (post.ID = meta2.post_id)
WHERE post.post_status = 'publish'
AND post.post_type = 'event'
AND post.post_author = '1'
AND (
( (meta.meta_key = 'date_0_start-date' AND meta.meta_value < '20140620') AND (meta2.meta_key = 'date_0_end-date' AND meta2.meta_value > '20140630') )
OR ( (meta.meta_key = 'date_0_start-date' AND meta.meta_value < '20140620') AND (meta2.meta_key = 'date_0_end-date' AND meta2.meta_value >= '20140620') )
OR ( (meta.meta_key = 'date_0_start-date' AND meta.meta_value > '20140620') AND (meta.meta_key = 'date_0_start-date' AND meta.meta_value <= '20140630') )
OR ( (meta.meta_key = 'date_0_start-date' AND meta.meta_value >= '20140620') AND (meta2.meta_key = 'date_0_end-date' AND meta2.meta_value <= '20140630') )
)
答案 0 :(得分:1)
您必须使用DISTINCT
之类的
SELECT DISTINCT ID, post_title
而不是
SELECT ID, post_title
答案 1 :(得分:1)
如果我正确理解了这个问题,我认为你需要将where子句中最后一个括号内的部分看作是这样的
meta.meta_key = 'date_0_start-date' AND meta.meta_value <= '20140630' and
meta2.meta_key = 'date_0_end-date' AND meta2.meta_value >= '20140620'
这应匹配任何与日期范围'20140620'重叠至'20140630'的事件