Mysql从3个表

时间:2017-12-04 18:44:54

标签: mysql database

我似乎无法弄清楚我需要做什么。我有3个表,其中包含所有信息。我已经确定了步骤,但我不确定现在要做什么,我需要显示特定作者列出的所有书籍。

我需要使用3个表

  • 作者 - 包含author_code,fname& L-NAME。
  • Book - 包含book_code,title,publisher_code,type,price和paperback
  • 写了 - 包含book_code,author_number和sequence

到目前为止,这是代码,我知道我应该使用作者姓名,但我无法理解。

SELECT BOOK_CODE
FROM WROTE 
WHERE AUTHOR_NUM = 20
LEFT JOIN AUTHOR
ON WROTE.AUTHOR_NUM = AUTHOR.AUTHOR_NUM;

2 个答案:

答案 0 :(得分:1)

你需要书的清单,所以从书表开始然后加入写作获取特定作者然后加入作者获取作者数据

SELECT BOOK_CODE
FROM BOOK
INNER JOIN  WROTE 
   ON WROTE.BOOK_CODE = BOOK.BOOK_CODE 
   AND WROTE.AUTHOR_NUM = 20
INNER JOIN AUTHOR
   ON AUTHOR.AUTHOR_NUM  = WROTE.AUTHOR_NUM 

答案 1 :(得分:0)

SELECT Book.book_code
FROM Book
LEFT JOIN WROTE ON WROTE.book_code = Book.book_code
LEFT JOIN AUTHOR ON AUTHOR.author_code = WROTE.author_number
WHERE AUTHOR.author_code IN (123,456)