我遇到的情况是我需要选择一个帖子,但显示的条件如下: -
一次发布所选条款。
我试过了: -
SELECT p.ID, p.post_title FROM wp_posts p
LEFT JOIN `wp_term_relationships` t
ON p.ID = t.object_id
LEFT JOIN `wp_term_taxonomy` tt
ON t.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.term_id =86
AND tt.term_id=39
GROUP BY t.object_id
HAVING COUNT( t.term_taxonomy_id ) =2
LIMIT 0,7
在这里,我想选择一个名为id 86&的帖子。这两个ID都在同一张表中。
答案 0 :(得分:0)
使用IN子句
SELECT p.ID
,p.post_title
FROM wp_posts p
LEFT JOIN 'wp_term_relationships' t ON p.ID = t.object_id
LEFT JOIN 'wp_term_taxonomy' tt ON t.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.term_id IN (86,39)
GROUP BY t.object_id
HAVING COUNT(t.term_taxonomy_id) = 2 LIMIT 0,7
答案 1 :(得分:0)
您可以按照以下方式编写您的where条件:
WHERE tt.term_id in (86,39)
答案 2 :(得分:0)
这些表之间有什么关系?
这个选择有效,但我认为你可以尝试另一种方式,你有TAG而不是代码吗?无论如何,检查一下。
SELECT
p.ID
FROM
wp_posts p
LEFT JOIN wp_term_relationships t ON (p.ID = t.object_id)
WHERE
exists (
SELECT tt.term_taxonomy_id FROM wp_term_taxonomy tt
WHERE tt.term_taxonomy_id = t.term_taxonomy_id
and tt.term_id in(86,39)
)
group by p.ID
having count(p.ID) = 2