MySQL调用用户名来显示而不是ID?

发布于 2024-08-25 05:36:08 字数 600 浏览 8 评论 0原文

我有一个用户表、书籍表和作者表。一个作者可以拥有很多本书,而一个用户也可以拥有很多本书。 (这就是我的数据库当前的设置方式)。因为我对到目前为止我的设置还很陌生,所以我的设置就像访问作者页面,然后查看作者的所有书籍。单本书的详细信息都显示在这个新页面上...我可以获得输出以显示与该书关联的用户 ID,但不是用户名,这也适用于作者姓名,我可以将作者 ID 发送到显示,但不显示名称,因此在下面的查询中的某个位置我没有调用正确的值:

SELECT users.user_id, 
       authors.author_id, 
       books.book_id, 
       books.bookname, 
       books.bookprice, 
       books.bookplot 
  FROM books 
INNER JOIN authors on books.book_id = authors.book_id 
INNER JOIN users ON books.book_id = users.user_id 
 WHERE books.book_id=" . $book_id;

有人可以帮我纠正这个问题,以便我可以显示与该书相关的作者姓名和用户名!感谢您的帮助:)

I have a users table, books table and authors table. An author can have many books, while a user can also have many books. (This is how my DB is currently setup). As I'm pretty new to So far my setup is like bookview.php?book_id=23 from accessing authors page, then seeing all books for the author. The single book's details are all displayed on this new page...I can get the output to display the user ID associated with the book, but not the user name, and this also applies for the author's name, I can the author ID to display, but not the name, so somewhere in the query below I am not calling in the correct values:

SELECT users.user_id, 
       authors.author_id, 
       books.book_id, 
       books.bookname, 
       books.bookprice, 
       books.bookplot 
  FROM books 
INNER JOIN authors on books.book_id = authors.book_id 
INNER JOIN users ON books.book_id = users.user_id 
 WHERE books.book_id=" . $book_id;

Could someone help me correct this so I can display the author name and user name both associated with the book! Thanks for the help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

無處可尋 2024-09-01 05:36:08

我认为正确的连接条件类似于(更改为粗体)

SELECT users.user_id,
**用户.用户名,**
作者.author_id,
**作者.作者姓名,**
books.book_id,
books.书名,
书籍.bookprice,
books.bookplot
来自书籍
INNER JOIN 书籍上的作者。**author_id** = 作者。**author_id**
INNER JOIN 用户在书籍上。**user_id** = users.user_id
WHERE books.book_id=" . $book_id;

或者您可以使用不太详细的语法进行简单的内部联接。

SELECT users.user_id,
users.用户名,
作者.author_id,
作者.author_name,
books.book_id,
books.书名,
书籍.bookprice,
books.bookplot
来自书籍、作者、用户
其中 books.author_id =authors.author_id
和 books.user_id = users.user_id
和 books.book_id=" . $book_id

I think the right join conditions would be something like (changes bolded)

SELECT users.user_id,
**users.user_name,**
authors.author_id,
**authors.author_name,**
books.book_id,
books.bookname,
books.bookprice,
books.bookplot
FROM books
INNER JOIN authors on books.**author_id** = authors.**author_id**
INNER JOIN users ON books.**user_id** = users.user_id
WHERE books.book_id=" . $book_id;

Or you can use the less verbose syntax for simple inner joins.

SELECT users.user_id,
users.user_name,
authors.author_id,
authors.author_name,
books.book_id,
books.bookname,
books.bookprice,
books.bookplot
FROM books, authors, users
where books.author_id = authors.author_id
and books.user_id = users.user_id
and books.book_id=" . $book_id

帅冕 2024-09-01 05:36:08

如果我将名称字段添加到选择查询中将会有所帮助:D

It would help if I added the name fields into the select query :D

赠我空喜 2024-09-01 05:36:08

这两个连接在一起:

INNER JOIN authors on books.book_id = authors.book_id 
INNER JOIN users ON books.book_id = users.user_id 

也意味着authors.book_id = users.user_id,从表面上看,这是没有意义的。当然,您发布的代码不是您实际使用的代码......?

These two joins together:

INNER JOIN authors on books.book_id = authors.book_id 
INNER JOIN users ON books.book_id = users.user_id 

also imply authors.book_id = users.user_id which, on the face of it, makes no sense. Surely the code you've posted is not the code you're actually using...?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文