对于我来说,使用wordpress wp_post表和wp_postmeta表以及wp_user表为Wordpress站点构建以下查询是一个真正的挑战。它工作得很好,但它包含了很多重复的陈述。我不确定如何简化(或者如果可以)。任何简化和消除重复的提示都将受到赞赏。
表包含如下数据:
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 a.*
FROM (
SELECT 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.id = pm_to.post_id
AND pm_to.meta_key = '_fep_participants'
AND p.post_parent = 0
AND pm_to.meta_value <> p.post_author)
JOIN wp_postmeta pm_delete
ON (p.id = pm_delete.post_id)
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 pm_delete.meta_value <> '_fep_delete_by_1' AND uf.user_login = 'myname'
OR p.post_type = 'fep_message' AND pm_delete.meta_value <> '_fep_delete_by_1' AND ut.user_login = 'myname')
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
) a
UNION
SELECT b.*
FROM (
SELECT 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
AND pm_to.meta_key = '_fep_participants'
AND pm_to.meta_value <> p.post_author)
JOIN wp_postmeta pm_delete
ON (p.id = pm_delete.post_id)
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 pm_delete.meta_value <> '_fep_delete_by_1' AND uf.user_login = 'myname'
OR p.post_type = 'fep_message' AND pm_delete.meta_value <> '_fep_delete_by_1' AND ut.user_login = 'myname')
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
) b
它产生的结果是:
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
答案 0 :(得分:0)
谢谢Solaflare。这样做了。这是我的简化:
SELECT 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.id = pm_to.post_id
AND p.post_parent = 0) OR (p.post_parent = pm_to.post_id
AND p.post_parent <> 0))
AND pm_to.meta_key = '_fep_participants'
AND pm_to.meta_value <> p.post_author)
JOIN wp_postmeta pm_delete
ON (p.id = pm_delete.post_id)
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 pm_delete.meta_value <> '_fep_delete_by_1' AND uf.user_login = 'myname'
OR p.post_type = 'fep_message' AND pm_delete.meta_value <> '_fep_delete_by_1' AND ut.user_login = 'myname')
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