SQL question - multiple table select

Results 1 to 2 of 2

Thread: SQL question - multiple table select

  1. #1
    Join Date
    Dec 1969

    Default SQL question - multiple table select

    Table setup:<BR>tbl_Books<BR>-BookID<BR>-BookTitle<BR><BR>link_Books_Users<BR>-BookID<BR>-UserID<BR><BR>tbl_Users<BR>-UserID<BR>-FirstName etc<BR><BR>I have a multiple table select that currently looks like this:<BR><BR>SELECT BookID, BookTitle FROM tbl_Books, link_Book_Users<BR>WHERE tbl_Books.BookID = link_Books_Users.BookID <BR>AND link_Books_Users.UserID = 1<BR><BR>In this the User logs in and the SQL selects the BookIDs that the user has available to them (link_Books_Users), then returns the BookTitles. This is fine however I need it to return every BookTitle in the tbl_Books so that I can list them all on the page with a checkbox beside them and for the Books that relate to the particular User I want the checkbox to be &#039;checked&#039;<BR><BR>At the moment it only returns the BookTitles that are recorded in the link_Books_Users table.<BR><BR>I was thining of using this SQL and gettin the Titles that the User has linked to them, storing them in an array, then selecting all the BookTitles with a very simple SELECT SQL and adding those to the array - resulting in a list of all the books with the user-related books duplicated - is this possible??<BR><BR>I basically want to retrieve all data from a table and then relate SOME of that data to another table, and if it doesn&#039;t properly relate, it just returns empty fields or something...<BR><BR>Any help would be much appreciated. <BR>Thanks! Ben

  2. #2
    Join Date
    Dec 1969

    Default RE: SQL question - multiple table select

    Do a left join:<BR><BR>SELECT * FROM tbl_Books <BR>LEFT JOIN link_Books_Users<BR>tbl_Books.BookID = link_Books_Users.BookID <BR>WHERE link_Books_Users.UserId=1<BR><BR>This will give you every book, but only a new value for UserId when the book exists for that user.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts