Results 1 to 4 of 4

Thread: [RESOLVED] How to create a query with columns from two tables

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    40

    Resolved [RESOLVED] How to create a query with columns from two tables

    OK i have two tables one is called Book Table and has these attributes ,Book Name, Author, ISBN, Book condition,Genre,Copies)

    the second table is called Reservation table and has these attributes (Reserver ID(PK), Reserver Name, Book ID(FK), Book Name, Check out date, Due date, Return date)

    I want to make a query of the two tables but i only want the query to displays the Book name, Book ID, Author Reserver name,Reserver ID [/COLOR]For the Books that are being reserved in the Reservation table. For example if a book in my book table has the Book ID of 1 and i enter this Book ID in my reservation table (which is a foreign key in this table) i want the query to only displays the information of the book with that Book iD along with the reserver name and reserver ID. This way i can see the reserver name,reserver ID,Book Name,Book ID, and Author in one row. <~~this will be the person who reserved the book and some of the attributes of the book which are the Book Name, Book ID, and author.


    I am using a sql table in visual basic
    Last edited by Srut; Nov 22nd, 2017 at 10:15 PM.

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

    Re: How to create a query with columns from two tables

    Firstly, please don't make your entire post bold. It just makes it harder to read. Bolding text should be done for emphasis and you don't need to emphasise an entire post. The default font, colour, etc, is the default for a reason. You should only add extra formatting to specific parts of your post for specific reasons.

    As for your question, what you want is called a join. If you search the web for "sql join" then you'll find plenty of information and examples. Note that an inner join will only retrieve data where there are related rows in both tables whereas an outer join will retrieve all data from one table or the other, even if there isn't a related row in the other table.

    By the way, you are not using a "sql table". SQL is the language you use to write queries for almost all databases, be it SQL Server, MySQL, SQLite, PostgreSQL or any other that may or may not include SQL in the name. If you actually mean that your database is SQL Server, which is Microsoft's enterprise-grade RDBMS, then that's what you should say. Each database has a slightly different flavour of the SQL language so queries to do the same thing will vary from one to another at times. That said, the rules for performing joins should be fairly consistent. If you're using SQL Server though, it wouldn't hurt to include "sql server" or "t-sql" in your search keywords. T-SQL is short for Trasact SQL and is the name for the specific flavour of SQL used in SQL Server.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    40

    Re: How to create a query with columns from two tables

    OK sorry about that misunderstanding on my part and i found out how to do the join thank you

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

    Re: How to create a query with columns from two tables

    Quote Originally Posted by Srut View Post
    sorry about that misunderstanding on my part
    If you're referring to the "sql table" bit, you're not alone and it's often people who should know better who are to blame. As I said, a lot of people just use "sql" when they actually mean "SQL Server". That's fine in certain contexts where everyone knows what you mean but in other contexts it can create confusion and those who should know better often lead those who don't into bad habits. There have been times when I've read "sql" and haven't bothered to make this distinction and assumed they mean SQL Server only to find out that they don't. That's a waste of everyone's time, so I always try to make the distinction myself and encourage others to do the same.

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