我想从我的posts
表检索结果与以下查询。我想选择帖子,其中声明的where部分中的两个条件都是合格的。我如何检索结果与分组在哪里查询
其中术语类型类别,是CategoryTest
其中术语类型标签,是TagTest
WHERE IN
条件目前包含一个元素,但可能是更多的。 这两个WHERE
给出了结果,如果其他不存在。 所以每个人独自工作或与OR
也工作。它回到了同一篇文章,ID为33 - 所以我认为WHERE组没有正确设置。
SELECT `posts`.*
FROM `posts`
LEFT JOIN `post_term` ON `posts`.`id` = `post_term`.`post_id`
LEFT JOIN `terms` ON `terms`.`id`= `post_term`.`term_id`
LEFT JOIN `meta` ON `meta`.`post_id` = `posts`.`id`
WHERE (
`terms`.`type` = 'category'
AND `terms`.`name` IN('CategoryTest')
)
AND (
`terms`.`type` = 'tag'
AND `terms`.`name` IN('TagTest')
)
表定义: 条款表 +----+--------------+--------------+----------+ | id | name | slug | type | +----+--------------+--------------+----------+ | 31 | CategoryTest | CategoryTest | category | | 32 | TagTest | TagTest | tag | +----+--------------+--------------+----------+
帖子表: +----+-------+---------+---------+-----------+------------+------------+ | id | title | content | user_id | parent_id | created_at | updated_at | +----+-------+---------+---------+-----------+------------+------------+ | 33 | HEllo | | 0 | 0 | NULL | NULL | +----+-------+---------+---------+-----------+------------+------------+
post_term +----+---------+---------+ | id | post_id | term_id | +----+---------+---------+ | 73 | 33 | 31 | | 74 | 33 | 32 | +----+---------+---------+
你说“或”的作品。那为什么不使用它? –
您可以显示术语表定义吗?或者更好一些示例数据 – Mihai
我想要检索结果,其中两个条件都通过,而不仅仅是其中一个。 – Iamzozo