Results 1 to 11 of 11

Thread: Query Tables

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    30

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Query Tables

    Yes, it is possible. You should look into joins, particularly inner joins in this case.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    30

    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

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Query Tables

    Quote Originally Posted by nigeloliveros View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    30

    Re: Query Tables

    Quote Originally Posted by dday9 View Post
    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?

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    30

    Re: Query Tables

    Quote Originally Posted by dday9 View Post
    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..

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    30

    Re: Query Tables

    Quote Originally Posted by dday9 View Post
    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.

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Query Tables

    With the code that I provided...
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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
  •  



Click Here to Expand Forum to Full Width