那是那些日子之一,我只是想不通。
我有以下查询:
SELECT * FROM wp_posts JOIN wp_term_relationships ON wp_term_relationships.object_id = wp_posts.ID WHERE term_taxonomy_id = 80
显然,这将选择类别ID 80中的所有帖子。我需要做的是选择类别80中的所有帖子,而不是类别109中的所有帖子。
我已经尝试过了,但是它只是从类别80中选择了相同的帖子。
SELECT * FROM wp_posts JOIN wp_term_relationships ON wp_term_relationships.object_id = wp_posts.ID WHERE term_taxonomy_id = 80 AND term_taxonomy_id = 109
这是表的结构:
wp_posts | ID | ------------ | 1 | | 2 | wp_term_relationships | object_id | term_taxonomy_id | |-----------|------------------| | 1 | 80 | | 2 | 80 | | 1 | 109 |
object_id与post_id匹配
该查询应仅返回ID 2,因为ID 1在80和109中。
我知道我已经做了一百万遍了,但是我一辈子都做不到。有什么帮助吗?
你可以写:
SELECT * FROM wp_posts JOIN wp_term_relationships ON wp_term_relationships.object_id = wp_posts.ID WHERE term_taxonomy_id = 80 AND wp_posts.id NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id = 109 ) ;
(见搂13.2.10.3 “与子查询ANY,IN或SOME”在 MySQL的5.6参考手册 )。
ANY
IN
SOME