Hello, I'm trying to figure out what to do here. Look at these tables:
users
id, username, password
books
id, title
users_books
bookID, userID
How would one join these together? If I for example wanted to see user 47's books?
Hello, I'm trying to figure out what to do here. Look at these tables:
users
id, username, password
books
id, title
users_books
bookID, userID
How would one join these together? If I for example wanted to see user 47's books?
If you are using SQL Server, this query should work.
select books.title
from books, users_books
where books.id = users_books.bookID and users_books.id = 47
select b.title from users_books ub
join books b on b.id = ub.bookID
where ub.userID = 47
Hi
Well, if it is a homework then instead of giving fish everyday, teaching how to fish would be more helpful.
I hope the following references would aid in understanding SQL joins-
http://www.sql-tutorial.net/SQL-JOIN.asp
http://www.tizag.com/sqlTutorial/sqljoin.php
As far as your join query is concerned, response given by 'm-sharp' should give you the correct answer.
cheers
Since none of the other answers are joining three tables (your question heading states three but your body only needs two), I'll give you a three table version.
To do that I assume you have the username of the user rather than the ID. In that case, you can do:
select
u.id as user_id,
u.username as user_name,
b.id as book_id,
b.title as book_title
from
users u,
books b.
user_books ub
where u.username = 'username-of-user-47'
and u.id = ub.userID
and b.id = ub.bookID
This sort of thing can also be done with explicit JOIN
s but I prefer the "old school" SQL ways especially when the DBMS is clever enough to optimize it in the same manner, such as my beloved DB2/z :-)