gpt4 book ai didi

mysql - 从多对多关系中获取数据

转载 作者:搜寻专家 更新时间:2023-10-30 20:05:24 25 4
gpt4 key购买 nike

我想从书表中获取某个作者所有书籍的标题和主题表中的主题名称

这是表的关系

enter image description here

enter image description here

我试图从表格中选择标题和主题,但我无法让它处理多对多关系

SELECT book_title,subject_name FROM book,subject WHERE $subject_ID = subject_ID INNER JOIN book_author ON author_ID = '$author_ID'

我想提出两个单独的问题,如果有人能帮助我,我会很高兴。

最佳答案

试试这个:

SELECT book_title, subject_name
FROM Book
INNER JOIN Book_Author ON Book.book_ISBN = Book_Author.book_ISBN
INNER JOIN Author ON Book_Author.author_ID = Author.author_ID
INNER JOIN Subject ON Subject.subject_ID = Book.subject_ID
WHERE author_lastname = [whatever];

发布模型的工作做得很好 :)

根据具体需要进行编辑:

SELECT book_title, subject_name
FROM Book
INNER JOIN Book_Author ON Book.book_ISBN = Book_Author.book_ISBN
INNER JOIN Subject ON Subject.subject_ID = Book.subject_ID
WHERE author_ID = '11';

顺便说一下,您有一个 “Column 'author_ID' in where clause is ambiguous” 因为该列同时出现在 Book_Author 和 Author 中。这就是为什么你必须在它前面加上表名:)

关于mysql - 从多对多关系中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11064275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com