Skip to main content
Known Participant
April 1, 2013
Answered

How to display records from a different table when you query an initial table

  • April 1, 2013
  • 19 replies
  • 2437 views

Hi all,

Currently I have a problem. I have three tables.

The first table is user with the attributes username (primary key), password, Full name etc.

The 2nd table is books with the attributes ISBN (primary key), Title, author, publisher etc.

The last table is reservation with the attributes reservation number (primary key), username (foreign key in reservation table as it is a primary key from user table), ISBN (foreign key in reservation table as it is a primary key from book table), number of orders, and collection date.

When I query my database by using the username that I have, I can display all the records related to the username from my reservation table.

May I ask how do I also display the Tite and Author beside the results?

Please provide detailed steps as I am quite new in this.

Thanks!

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer bregent

You do that in SQL using joins. For example

SELECT user.username, title, author FROM user, books, reservation

WHERE

user.username = reservation.username AND

books.ISBN = reservation.ISBN

That's an example of a join in the WHERE clause using pre ANSI92 syntax which I prefer. The newer style uses joins in the FROM clause. Google SQL joins for more details.

19 replies

bregentCorrect answer
Participating Frequently
April 1, 2013

You do that in SQL using joins. For example

SELECT user.username, title, author FROM user, books, reservation

WHERE

user.username = reservation.username AND

books.ISBN = reservation.ISBN

That's an example of a join in the WHERE clause using pre ANSI92 syntax which I prefer. The newer style uses joins in the FROM clause. Google SQL joins for more details.

IVYYAuthor
Known Participant
April 1, 2013

Hi bregent,

Thanks alot! I managed to show results from other tables after u point out to me it is SQL joins.

May you guide me on mysql_insert_id which you have comented on my previous 2 discussions? I searched alot on youtube and google but seem not able to understand.

Thanks!

IVYYAuthor
Known Participant
April 1, 2013

Hi,

May I ask how do i get a multiplied result?

For example, I have no.of orders from the reservation table and Price from the books table. How can i find the multiplication of no. of orders and price in mysql. Thanks!