例如,如果您有一个表books
,并且您希望按特定名称查找所有书籍,那么请获取这些书籍的作者并研究表中所有条目的books
表。例如,如果您搜索了标题“All About Pirates”,然后有2本书带有标题,一本是“Jane Doe”而另一本是“Joe Smith”,那么这些作者将再次搜索所有标题。 / p>
select *
from books b
join books b2 on b.author = b2.author
where title = 'All About Pirates'
答案 0 :(得分:3)
使用这样的子查询: SELECT * FROM books WHERE作者IN(SELECT author FROM books WHERE title ='All About Pirates');
答案 1 :(得分:2)
您的查询几乎正确无误。您需要在where子句中指定title的别名:
select b2.*
from books b join
books b2
on b.author = b2.author
where b.title = 'All About Pirates'
原始查询无法解析,因为标题不明确。在此查询中,b2
是具有匹配作者的书籍列表。
答案 2 :(得分:1)
您可以将表格加入选择的结果。
例如,您可以获得与此书匹配的作者:
SELECT author FROM books WHERE title="All About Pirates" GROUP BY author;
所以你可以将书籍表加入到这样的作者表中:
SELECT books.*
FROM books
JOIN (SELECT author FROM books WHERE title="All About Pirates" GROUP BY author) AS r
ON books.author=r.author;