0

我有 3 个表格帖子、术语和关系。

帖子表:

ID     title      
1      abc
2      cdf

术语表:

term_id  slug
1        jeans
2        shirts

关系表:

object_id    taxonomy_id
1             1
2             1

我曾经尝试列出与“牛仔裤”相关的标题的 MySQL 查询

SELECT posts.title 
FROM posts, terms, relationships
WHERE (SELECT terms.term_id FROM terms WHERE terms.slug LIKE '%jeans%')
AND (SELECT relationships.object_id FROM relationships WHERE terms.term_id = relationships.taxonomy_id)
AND (posts.ID = relationships.object_id)

它给了我错误 #1242 - 子查询返回超过 1 行。我怎样才能解决这个问题?

4

3 回答 3

0

试试这个:

SELECT posts.title
FROM posts INNER JOIN relationships ON (posts.ID = relationships.object_id)
           INNER JOIN terms ON (terms.term_id = relationships.taxonomy_id)
WHERE terms.slug LIKE '%jeans%';

我没有你的桌子,所以我无法测试,但我认为这应该可行。

于 2012-05-17T00:25:04.327 回答
0

尝试这样的事情:

SELECT p.title 
  FROM posts p
  JOIN relationships r
    ON r.object_id = p.ID
  JOIN terms t
    ON t.term_id = r.taxonomy_id 
 WHERE t.slug LIKE '%jeans%'
于 2012-05-17T00:26:08.490 回答
0
SELECT posts.title
FROM posts 
    INNER JOIN relationships ON (posts.ID = relationships.object_id)
    INNER JOIN terms ON (terms.term_id = relationships.taxonomy_id)
WHERE 
    (terms.slug LIKE '%jeans%')
AND 
    (terms.term_id = relationships.taxonomy_id)
AND 
    (posts.ID = relationships.object_id)
于 2012-05-17T00:27:09.643 回答