Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Complex Select Statement.
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Complex Select Statement.

 Post Reply Post Reply
Author
davidshq View Drop Down
Senior Member
Senior Member


Joined: 29 July 2003
Location: United States
Status: Offline
Points: 299
Post Options Post Options   Thanks (0) Thanks(0)   Quote davidshq Quote  Post ReplyReply Direct Link To This Post Topic: Complex Select Statement.
    Posted: 10 June 2005 at 5:04pm
Lets say I have two tables. The first table is called Books. It contains the columns ID, Title, Author, and Popularity. The second is called BookRatings. It contains the columns ID, userID, bookID, and Popularity. Now, lets say I have two users, Jane and Joe. There are three books in the library: About HTML, About CGI, About ASP. Jane reads the first two books and leaves a rating of 8 and 9 respectively. Joe reads the first and last book and leaves a rating of 7 and 10 respectively.
Now, when Joe signs in he should be able to go to a page that shows him the books he has read and their rating. Jane should be able to do the same thing. How can this be accomplished.
If one does...:
SQL Select * FROM Books B LEFT JOIN BookRatings BR ON B.Popularity=BR.Popularity WHERE ID=varID
One gets the results, but the database doesn't know which records to pull, b/c both Jane and Joe have rated some of the same books.
If one does...:
SQL Select * FROM Books B LEFT JOIN BookRatings BR ON B.Popularity=BR.Popularity WHERE ID=varID and BR.userID=varUser and BR.bookID=varbookID
This works fine unless the user hasn't rated the book, then it gives back no answer at all.
So, what is needed is something that does something like:
SQL Select * FROM Books B LEFT JOIN BookRatings BR ON B.Popularity=BR.Popularity WHERE ID=varID AND IF BR.userID=varUser and BR.bookID=varBookID then these last two conditions two.
But I am at a complete lost as to how to accomplish this.
David.
Back to Top
davidshq View Drop Down
Senior Member
Senior Member


Joined: 29 July 2003
Location: United States
Status: Offline
Points: 299
Post Options Post Options   Thanks (0) Thanks(0)   Quote davidshq Quote  Post ReplyReply Direct Link To This Post Posted: 13 June 2005 at 9:54am
Here is the code I came up with to fix my problem in the end:
SqlDataSource2.SelectCommand = "SELECT * FROM Game G LEFT JOIN GamePopularity GP ON G.YourPopularity = GP.Popularity WHERE (G.ID=" & ID & " AND GP.gID=" & ID & " AND GP.uID LIKE '" & varUserName & "') OR (G.ID=" & ID & " AND GP.gID IS NULL)"
David.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.