【发布时间】:2026-02-23 19:10:01
【问题描述】:
我在数据库中有两个简单的表
论文
ID Title Author
1 A study of turtles Mary
2 Dietary habits of ducks Kate
3 Similarities of turtles and cats Fred
关键词
ID Keyword
1 turtles
2 ducks
3 turtles
3 cats
我想选择所有具有关键字“海龟”的论文。也就是说,它会返回
ID Title Author
1 A study of turtles Mary
3 Similarities of turtles and cats Fred
或
ID Title Author Keyword
1 A study of turtles Mary turtles
3 Similarities of turtles and cats Fred turtles
我不关心是否包含关键字标题。
我认为我需要使用别名来使用选择和内连接 - 但语法不正确。
我试过了
select * from (select papers.*, keywords.* from papers inner join keywords on papers.id = keywords.id) where keyword = "turtles";
这给出了错误Every derived table must have its own alias
我已经尝试关注What is the error "Every derived table must have its own alias" in MySQL?
select * from (select papers.*, keywords.* from
(papers inner join keywords on papers.id = keywords.id) as T)
as T)
where keyword = "turtles";
返回语法错误。
我知道有很多类似的查询,但我是 MySQL 新手,对问题/示例感到困惑。
编辑:澄清我想从表关键字(本例中的 1 和 3)中返回与关键字“turtle”匹配的 ID,然后从论文表中选择与这些 ID 对应的行。我不想在标题中找到关键字。
【问题讨论】:
-
不要使用相同的别名。你已经尝试改变你的表的别名了吗?
-
对不起@dwir182 我不确定你在问什么。您能否改写您的问题 - 我对 MySQL 很陌生,所以如果我遗漏了一些明显的东西,我们深表歉意。
标签: mysql inner-join alias