Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inner join to select data from multiple tables?

Status
Not open for further replies.

DomTrix

Programmer
Dec 28, 2004
94
GB
The usage I have seen for INNER JOIN is when using a condition on cross table references to select data from one table. Yet the book I am using suggests in its intro to IJ that you can select data from multiple tables using INNER JOIN. Is this correct?

What I would like is the optimal solution to this problem:

I have table1 with say bookID, bookTitle, authorID.
I have table2 with authorID, authorName.

I want to supply my 'getbookData' query with a bookID and have it select the bookTitle and the authorName (not the authorID) for each book in the table.

Is this doable with INNER JOIN? And with one SELECT statement?

Thanks in advance

DT
 
but what is that you want to join these two tables...your query looks something like this...but what is the blah in your tables that we can join both tables on??

SELECT t1.bookTitle, t2.authorname from
table1 t1 INNER JOIN table2 t2 ON
t1.blah=t2.blah WHERE t1.bookID='whatever'

-DNG

 
Try this

Select t1.BookID, t1.BookTitle, t2.AuthorName
from table1 t1
inner join table2 t2 on t1.authorid = t2.authorid

Just remember that this query will only bring matching records from both table, If you want to get all the book title irrespective to author's name then u can write

Select t1.BookID, t1.BookTitle, t2.AuthorName
from table1 t1
Left join table2 t2 on t1.authorid = t2.authorid

Thanks
Shafiq
 
Brilliant thankyou guys thats cleared it up nicely :)

DT

(yes the blah was authorID) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top