我看了类似的问题,但没有找到正确的语法。
我有这些桌子:
引用
-报价单
-作者id
-引文
作者
-作者id
-作者
主题地图
-主题映射id
-主题工程
按主题引用
-报价单
-主题映射id
我需要从table Quotes中获取'quote-ENG',从table Authors中获取'author',其中'topic-ENG'='age'。
我可以得到“报价”值

SELECT quote_ENG
FROM Quotes, TopicMap, QuotesByTopic
WHERE TopicMap.topics_ENG='age'
  and QuotesByTopic.topicmap_id = TopicMap.topicmap_id
  and QuotesByTopic.quotes_id = Quotes.quotes_id

……但我错过了最后一段,也没有得到“作者”的价值:
SELECT author, quote_ENG
FROM Authors, Quotes, TopicMap, QuotesByTopic
WHERE TopicMap.topics_ENG = 'age'
  and Authors.author_id = Quotes.author_id
  and QuotesByTopic.topicmap_id = TopicMap.topicmap_id
  and QuotesByTopic.quotes_id = Quotes.quotes_id

谢谢你的帮助

最佳答案

请改为:

SELECT
  q.quote_ENG,
  a.author
FROM QuotesByTopic  AS qt
INNER JOIN TopicMap AS t ON qt.topicmap_id = t.topicmap_id
INNER JOIN Quotes   AS q ON qt.quotes_id   = q.quotes_id
INNER JOIN Authors  AS a ON a.author_id    = q.author_id
WHERE t.topics_ENG = 'age'

关于mysql - 使用MySQL连接四个表并从其中两个表获取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18559418/

10-11 03:31