我需要在将表连接到其他表之前对其进行聚合。
wp_postmeta GROUP BY wp_postmeta.post_id
有可能吗?我应该把它放在哪里?这是我的代码:
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-type'
AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3')
AND
(
wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value <> '111'
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '22'
OR wp_postmeta.meta_key <> 'include' AND wp_postmeta.meta_key <> 'exclude'
)
在测试ruakh示例时找到了一种新方法。我不知道它为什么会起作用,但确实如此:
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-type'
AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3')
AND NOT wp_posts.id IN # ADDED NOT
(
SELECT wp_postmeta.post_id
FROM wp_postmeta
WHERE wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value = '111' #SINCE NOT ABOVE I CHANGED <> to =
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value <> '22' #SINCE NOT ABOVE I CHANGED = to <>
#I DELETED THIS LINE
)
现在,如果未设置include或exclude,查询将返回数据。如果是,则检查ID。
对我的更改有何评论?
更多改进:
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-type'
AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3')
AND wp_posts.id NOT IN
(
SELECT wp_postmeta.post_id
FROM wp_postmeta
WHERE wp_postmeta.meta_key = 'exclude'
AND wp_postmeta.meta_value IN
('$id', '$id,%', '%,$id,%', '%,$id')
OR wp_postmeta.meta_key = 'include'
AND wp_postmeta.meta_value NOT IN
('$id', '$id,%', '%,$id,%', '%,$id')
wp_postmeta表的示例:
Senario 1 - 只有身份标识为18的网页才能获取数据:
meta_id post_id meta_key meta_value
1 30 include 18
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
Senario 2-id为18的页面无法获取数据:
meta_id post_id meta_key meta_value
1 30 exclude 18
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
Senario 1 - 所有页面都应获取数据:
meta_id post_id meta_key meta_value
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
答案 0 :(得分:2)
我认为你根本不应该真正加入wp_postmeta
,因为你实际上并不需要任何东西;相反,您应该使用IN
或EXISTS
子句执行“半连接”。例如:
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-type'
AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3')
AND wp_posts.id IN
(
SELECT DISTINCT wp_postmeta.post_id
FROM wp_postmeta
WHERE wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value <> '111'
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '22'
OR wp_postmeta.meta_key <> 'include' AND wp_postmeta.meta_key <> 'exclude'
)
答案 1 :(得分:0)