Forum Moderators: open

Message Too Old, No Replies

I need help re-writing my SQL join's to include 1 more table

         

midoriweb

10:56 am on Jan 31, 2007 (gmt 0)

10+ Year Member



I'm going to include my current code posted below. This code is pulling information from my Products table and my Categories_Products_Link table. The key for both this tables is ProductID. The problem is, I want to include another table in this "SQL join" I currently have. The new table I want to include as well is Products_Memos. The Key for this table is the same as the others, ProductID. Below is my code... any help would really be appriciated (I'm played around with it for hours but my knowledge of SQL joins is very limited).

*********************************************
SQL = "SELECT " & Config_ProductsTable_Fields
SQL = SQL & " FROM Products_Joined P " & SQL_LockType("NOLOCK")

If Current_Category <> "" Then
SQL = SQL & " WHERE p.ProductID IN ( "
SQL = SQL & " SELECT p2.ProductID FROM Products p2 INNER JOIN Categories_Products_Link cpl ON cpl.ProductID = p2.ProductID WHERE " & Replace(AddSQLConstraints(),"P.","p2.") & " "

Dim Refinement_CategoryIDs_Array, Refinement_Total_Cats, Refinement_CategoryIDs_CSV
Refinement_CategoryIDs_Array = Split(Current_Category & Refinement_CategoryIDs,",")
Refinement_Total_Cats = Ubound(Refinement_CategoryIDs_Array)+1
Refinement_CategoryIDs_CSV = Current_Category & Refinement_CategoryIDs

SQL = SQL & " AND cpl.CategoryID IN (" & Refinement_CategoryIDs_CSV & ")"
SQL = SQL & " GROUP BY p2.ProductID "
SQL = SQL & " HAVING Count(cpl.CategoryID) = " & Refinement_Total_Cats
SQL = SQL & " ) "
End If

[edited by: midoriweb at 11:04 am (utc) on Jan. 31, 2007]

syber

12:37 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



When you join two tables together you are internally creating a new table that has rows from both joined tables. Whan you join in a third table you are joining it to the new internal table and so on.

So it should look like this:

SELECT p2.ProductID
FROM Products p2 INNER JOIN Categories_Products_Link cpl
ON cpl.ProductID = p2.ProductID
INNER JOIN Products_Memos pm
ON pm.ProductID = p2.ProductID

midoriweb

9:02 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



Hi Syber,

Thanks for the help :)

So I tried the following code but it's not working... what am I doing wrong?

*****************************************

SQL = SQL & " WHERE p.ProductID IN ( "
SQL = SQL & " SELECT p2.ProductID FROM Products p2 INNER JOIN Categories_Products_Link cpl ON cpl.ProductID = p2.ProductID WHERE " & Replace(AddSQLConstraints(),"P.","p2.") & " INNER JOIN Products_Memos pm ON pm.ProductID = p2.ProductID"

justageek

9:16 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the error you are getting?

JAG

midoriweb

9:22 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



Sorry... I should have included that :)

************************

Microsoft OLE DB Provider for SQL Server error '80040e14'

Incorrect syntax near the keyword 'INNER'.

SearchResults_preload.asp, line 573

***************************

Line 573 in that file is the following:

****************************

RS_Search.Open SQL, Conn, adOpenForwardOnly, adLockReadOnly

syber

3:01 am on Feb 1, 2007 (gmt 0)

10+ Year Member



All the JOINS have to be together before the WHERE clause.

midoriweb

7:45 am on Feb 1, 2007 (gmt 0)

10+ Year Member



Thanks Syber,

I I edited my code to the following and everything worked without any errors:

*************************
SQL = SQL & " WHERE p.ProductID IN ( "
SQL = SQL & " SELECT p2.ProductID FROM Products p2 INNER JOIN Categories_Products_Link cpl ON cpl.ProductID = p2.ProductID INNER JOIN Products_Memos pm ON pm.ProductID = p2.ProductID WHERE " & Replace(AddSQLConstraints(),"P.","p2.") & " "
*************************

However... now that I'm trying to call the column from the new table it's causing an error. The error I'm getting is the following:

*************************
Microsoft OLE DB Provider for SQL Server error '80040e14'

The column prefix 'pm' does not match with a table name or alias name used in the query.

/SearchResults_preload.asp, line 573
*************************

The code I've changed is from this:

Call GenerateSearchQuery(Split_SearchText, "p.ProductKeywords")

to this:

Call GenerateSearchQuery(Split_SearchText, "pm.METATAG_Keywords")

That caused the error I posted above. I then tried to change my original code of joining tables in the "AddSQLContraints" section from this:

SQL = SQL & " SELECT p2.ProductID FROM Products p2 INNER JOIN Categories_Products_Link cpl ON cpl.ProductID = p2.ProductID INNER JOIN Products_Memos pm ON pm.ProductID = p2.ProductID WHERE " & Replace(AddSQLConstraints(),"P.","p2.") & " "

To this:

SQL = SQL & " SELECT p2.ProductID FROM Products p2 INNER JOIN Categories_Products_Link cpl ON cpl.ProductID = p2.ProductID INNER JOIN Products_Memos pm ON pm.ProductID = p2.ProductID WHERE " & Replace(AddSQLConstraints(),"P.","p2.","pm.") & " "

Doing so didn't cause an error by changing only that... but when I changed the code that called from the pm.METATAG_Keywords table it did throw an error.

Any ideas?

syber

5:39 pm on Feb 4, 2007 (gmt 0)

10+ Year Member



What table is METATAG_Keywords in?

If it's in Products_Joined, you will need to say P.METATAG_Keywords. If not, then you need to do the JOIN in the first FROM clause after Products_Joined P.

midoriweb

9:26 pm on Feb 4, 2007 (gmt 0)

10+ Year Member



METATAG_Keywords is in Products_Memos.

I changed:

SQL = SQL & " FROM Products_Joined P " & SQL_LockType("NOLOCK")

To:

SQL = SQL & " FROM Products_Joined P INNER JOIN Products_Memos pm ON pm.ProductID = p2.ProductID " & SQL_LockType("NOLOCK")

And got this error:

Microsoft OLE DB Provider for SQL Server error '80040e14'

Incorrect syntax near the keyword 'WITH'.

/store/incpages/SearchResults_preload.asp, line 573

Any ideas?

midoriweb

10:03 am on Feb 5, 2007 (gmt 0)

10+ Year Member



I got it working! Thanks again Syber for the help.

I changed this line:

SQL = SQL & " FROM Products_Joined P " & SQL_LockType("NOLOCK")

To this:

SQL = SQL & " FROM Products_Joined P INNER JOIN Products_Memos pm ON p.ProductID = pm.ProductID "

I am then able to call my METATAG_Keywords column from the Products_Memos table.

But... I have one last question... What does SQL_LockType("NOLOCK") do at the end of my original code? I had to remove it to get my new code working... but I can't figure out what that little piece of code actually does.

midoriweb

10:10 am on Feb 5, 2007 (gmt 0)

10+ Year Member



I also found that this works:

SQL = SQL & " FROM Products_Joined P " & SQL_LockType("NOLOCK") & " INNER JOIN Products_Memos pm " & SQL_LockType("NOLOCK") & " ON p.ProductID = pm.ProductID "

Is there a reason I should or shouldn't be using the SQL_LockType?