Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| Search | Today's Posts | Mark Forums Read
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 23rd, 2006, 10:28 AM
Authorized User
 
Join Date: Jun 2003
Location: Guildford, Surrey, United Kingdom.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Alexpizzoferro
Default Joining Three Tables

Hello all

From having an ID number of a field in a favs table, which is the same as that in the books table, I need to retrieve information about the book. I can get this to work but I also need to join to a third table, the user table by using the userID in the books table and joining to the user table to then retrieve informaiton about that user.

I hope this makes sense.
The code I have written is below, it should be evident from this what I want to do.

strSQL = "SELECT favs.ID, favs.objectID, favs.object_type, books.ID, books.userID, books.title, books.author, restrictedAccess_db.first_name, restrictedAccess_db.surname, restrictedAccess_db.email "
strSQL = strSQL & "FROM favs "
strSQL = strSQL & "LEFT JOIN books "
strSQL = strSQL & "ON favs.objectID = books.ID "
strSQL = strSQL & "JOIN restrictedAccess_db "
strSQL = strSQL & "ON books.userID = restrictedAccess_db.userID "
strSQL = strSQL & "WHERE favs.objectID = '" & u_ID & "' And favs.object_type = 'book' "
strSQL = strSQL & "ORDER BY books.title;"

This gives me the following error:

Syntax error (missing operator) in query expression 'favs.objectID = books.ID JOIN restrictedAccess_db ON books.userID = restrictedAccess_db.userID'.


I hope someone can help.

Thanks in advance.

http://www.loghopper.com
__________________
http://www.loghopper.com
Reply With Quote
  #2 (permalink)  
Old August 23rd, 2006, 12:29 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm that should be fine...I wrote this in query analyzer and it parsed ok

SELECT
    f.id,
    f.objectID,
    f.object_type,
    b.id,
    b.userID,
    b.title,
    b.author,
    r.firstName,
    r.surName,
    r.email
FROM
    favs f
LEFT JOIN
    Books b
ON
   f.objectID = b.ID
LEFT JOIN
   restrictedAccess_db r
ON
   b.UserID = r.UserID
WHERE
   f.objectID =@value AND f.object_type = 'book'
ORDER BY
   b.title

"The one language all programmers understand is profanity."
Reply With Quote
  #3 (permalink)  
Old August 31st, 2006, 04:13 PM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perhaps this line:
Code:
strSQL = strSQL    & "JOIN restrictedAccess_db "
should be changed to this:
Code:
strSQL = strSQL    & "INNER JOIN restrictedAccess_db "
Woody Z http://www.learntoprogramnow.com
Reply With Quote
  #4 (permalink)  
Old September 1st, 2006, 07:40 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The type of join they use is relative to the data they are trying to retrive; I favor Outer Joins (in my example a Left Join) so that I can deal with any data that may be NULL.

"The one language all programmers understand is profanity."
Reply With Quote
  #5 (permalink)  
Old September 1st, 2006, 03:50 PM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perhaps... and in this case, it is unclear that using a second outer join makes any sense, let alone the first one. Why bother bringing data back at all for the "favs" of a user who doesn't exist? Then you don't have to deal with the NULLS or anything else for that matter.

Woody Z http://www.learntoprogramnow.com
Reply With Quote
  #6 (permalink)  
Old September 1st, 2006, 04:08 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Again it is all strictly relative to what you are trying to do; there are instances where you need to do with NULL values, however, it is pointless to debate on this as we haven't a clue what the database looks like, values that should be returned, yadda yadda yadda. Good insight though woody.

"The one language all programmers understand is profanity."
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help joining tables and restricting records bren582 SQL Server 2005 3 January 18th, 2008 02:45 PM
joining tables pit_bait SQL Language 2 September 28th, 2007 07:29 AM
problem joining tables using vfpoledb in c# ACE2084 ADO.NET 2 April 1st, 2006 03:10 AM
joining 3 tables koskalgr MySQL 5 March 24th, 2006 09:52 AM
Joining two tables rajustha SQL Server 2000 1 December 7th, 2003 07:38 AM



All times are GMT -4. The time now is 11:20 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.