-
May 16th, 2018, 08:43 AM
#1
Thread Starter
Junior Member
Query Tables
I have table Borrower and A BookBorrowed both contains Borrower_ID , From Borrower , Borrower is PK .
I want to Select all borrower names , on the table of BookBorrowed. Is it possible.
-
May 16th, 2018, 08:53 AM
#2
Re: Query Tables
Yes, it is possible. You should look into joins, particularly inner joins in this case.
-
May 16th, 2018, 09:00 AM
#3
Re: Query Tables
I have moved this to the Database Development forum. Also, please specify what database and version you're using (MySql, Sql Server, Access, etc.).
Essentially you'd perform an inner join command in your From clause:
Code:
SELECT
`borrower`.`name`,
`book_borrowed`.`name`
FROM `borrower`
INNER JOIN `book_borrowed` ON `borrower`.`borrower_id` = `book_borrowed`.`borrower_id`;
Fiddle: Live Demo
-
May 16th, 2018, 09:31 AM
#4
Thread Starter
Junior Member
Re: Query Tables
https://dropmb.com/download/47da3489...9270b882c.html
Please look at my database.
I just want, that everytime theres new record in borrow_book I can get the Borrower ID and Borrower name
-
May 16th, 2018, 09:44 AM
#5
Re: Query Tables
Originally Posted by nigeloliveros
I just want, that everytime theres new record in borrow_book I can get the Borrower ID and Borrower name
Sweet, with the code that I provided you in Post #3 you can do just that with minimal changes.
-
May 16th, 2018, 10:02 AM
#6
Thread Starter
Junior Member
Re: Query Tables
Originally Posted by dday9
Sweet, with the code that I provided you in Post #3 you can do just that with minimal changes.
Thanks it works now.
But how can I delete duplicate borrower ID?
-
May 16th, 2018, 10:59 AM
#7
Re: Query Tables
Do you actually want to delete the rows that contain duplicate borrower_id values or do you just want to exclude them from the query?
-
May 17th, 2018, 01:41 AM
#8
Thread Starter
Junior Member
Re: Query Tables
Originally Posted by dday9
Do you actually want to delete the rows that contain duplicate borrower_id values or do you just want to exclude them from the query?
I just want to display 1 record only if theres two for that particulat borrower..
-
May 17th, 2018, 09:05 AM
#9
Re: Query Tables
OK, you can do that by using the GROUP BY clause on your borrower_id column:
Code:
SELECT
`borrower`.`name`,
`book_borrowed`.`name`
FROM `borrower`
INNER JOIN `book_borrowed` ON `borrower`.`borrower_id` = `book_borrowed`.`borrower_id`
GROUP BY(`borrower`.`borrower_id`);
Fiddle: Live Demo
Also, to help you understand some of the terminology, whenever you say that you want to delete value(s) generally that means that you literally want to remove them from the data table and not get them back. In this case, your followup question should have been:
How can I get distinct values if the borrower_id column has duplicates?
-
May 17th, 2018, 09:03 PM
#10
Thread Starter
Junior Member
Re: Query Tables
Originally Posted by dday9
OK, you can do that by using the GROUP BY clause on your borrower_id column:
Code:
SELECT
`borrower`.`name`,
`book_borrowed`.`name`
FROM `borrower`
INNER JOIN `book_borrowed` ON `borrower`.`borrower_id` = `book_borrowed`.`borrower_id`
GROUP BY(`borrower`.`borrower_id`);
Fiddle: Live Demo
Also, to help you understand some of the terminology, whenever you say that you want to delete value(s) generally that means that you literally want to remove them from the data table and not get them back. In this case, your followup question should have been:
How can I get distinct values if the borrower_id column has duplicates?
I just want to display 1 record for each duplicate record, but not necessarily delete the data in the tables.
-
May 17th, 2018, 10:17 PM
#11
Re: Query Tables
With the code that I provided...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|