我正在使用Wordpress并尝试构建一个查询,我需要包含和排除条件。我没有正确排除删除条件(请参阅结果和预期结果)。我想从我的查询中排除所有记录' _fep_delete_by_1'存在。我正在寻找其他选项来排除' _fep_delete_by_1'存在。
我的桌子
wp_posts
ID | post_author | post_parent | post_type | post_title | post_date
2258 163 0 fep_message a
2262 1 2258 fep_message re:a
2264 163 2258 fep_message re:a
1698 1 0 fep_message b
1692 1 0 fep_message c
wp_postmeta
meta_id | post_id | meta_key | meta_value
14696 2258 _fep_participants 1
14697 2258 _fep_participants 163
9819 1698 _fep_participants 163
9820 1698 _fep_participants 1
9759 1692 _fep_participants 163
9760 1692 _fep_participants 1
9815 1692 _fep_delete_by_1 1499496054
13751 1698 _fep_delete_by_163 1501044119
wp_user
ID | user_login
1 myname
163 theirname
我的查询
SELECT DISTINCT p.id, p.post_date, p.post_title, uf.user_login AS from_login, ut.user_login AS to_login
FROM wp_posts AS p
JOIN wp_postmeta pm_to
ON
((p.post_parent = pm_to.post_id
AND p.post_parent <> 0)
OR
(p.id = pm_to.post_id
AND p.post_parent = 0))
AND pm_to.meta_key = '_fep_participants'
AND pm_to.meta_value <> p.post_author
LEFT JOIN wp_postmeta pm_delete
ON
((pm_delete.post_id = p.post_parent AND pm_delete.meta_value = '_fep_delete_by_1')
OR
(pm_delete.post_id = p.id AND pm_delete.meta_value = '_fep_delete_by_1'))
LEFT JOIN wp_users AS uf ON uf.ID = p.post_author
LEFT JOIN wp_users AS ut ON ut.ID = pm_to.meta_value
WHERE (p.post_type = 'fep_message' AND uf.user_login = 'myname'
OR p.post_type = 'fep_message' AND ut.user_login = 'myname')
AND pm_delete.post_id IS NULL
AND p.post_date >= '2017-07-07 00:00:00'
AND p.post_date <= '2017-08-12 23:59:59'
ORDER BY p.post_date
它产生的结果是:
id |post_date |post_title |from_login |to_login
1692 2017-07-07 11:45:03 c myname theirname
1698 2017-07-08 16:28:18 b myname theirname
2258 2017-08-11 23:15:10 a theirname myname
2262 2017-08-12 16:48:05 re:a myname theirname
预期的结果是:
id |post_date |post_title |from_login |to_login
1698 2017-07-08 16:28:18 b myname theirname
2258 2017-08-11 23:15:10 a theirname myname
2262 2017-08-12 16:48:05 re:a myname theirname
我可能做错了什么,我该如何解决?