Connecting Tech Pros Worldwide Help | Site Map

combining 3 tables in a query

Familiar Sight
 
Join Date: Jul 2006
Posts: 181
#1: Feb 27 '09
Hi, Dont know if anyone can help but I need to combine three tables in a complex query. No idea how to go about it. All three work fine independantly but rather than have them split over the page I need them in one statement so I can use the query better. tblartists,tblpagetitle,tblCategories
Any pointers would be great.
Thanks in advance
Richard

Expand|Select|Wrap|Line Numbers
  1. lngCategoryID=CLng(Request("CategoryID"))
  2. If lngCategoryID <> "" And lngCategoryID <> 0 Then
  3.  
  4.     Set conn=Server.CreateObject("ADODB.Connection") 
  5.     conn.Open ConnectString
  6.     Set rs=Server.CreateObject("ADODB.Recordset")
  7.     rs.CursorLocation = 3
  8.  
  9.  
  10.     SQLQuery="Select artistid,categoryid,imagetitle,imagemedium,"
  11.     SQLQuery= SQLQuery & "imageprice,imagesize,imageyear,pictureurl,additional from tblartists Where CategoryID=" & Clng(lngCategoryID) & " "
  12.  
  13.  
  14. strSQL="SELECT artistname FROM tblpagetitle"
  15.  
  16.  
  17.  
  18. strSQL="SELECT tblCategories.CategoryID, tblCategories.CategoryName, "
  19. strSQL=strSQL & " Count(tblartists.artistID) AS CountOfartistID "
  20. strSQL=strSQL & " FROM tblCategories INNER JOIN tblartists ON "
  21. strSQL=strSQL & " tblCategories.CategoryID = tblartists.CategoryID "
  22. strSQL=strSQL & " GROUP BY tblCategories.CategoryID, tblCategories.CategoryName"
  23. strSQL=strSQL & " Order By tblCategories.CategoryName ASC "
  24.  
Dwagar's Avatar
Newbie
 
Join Date: Feb 2009
Location: Cleveland, Ohio
Posts: 6
#2: Feb 28 '09

re: combining 3 tables in a query


Try this out, it should work for you.

strSQL="SELECT tblCategories.CategoryID+' '+tblCategories.CategoryName+' '+"
strSQL=strSQL & "Count(tblartists.artistID) AS CountOfartistID "
strSQL=strSQL & " FROM tblCategories INNER JOIN tblartists ON "
strSQL=strSQL & " tblCategories.CategoryID = tblartists.CategoryID "
strSQL=strSQL & " GROUP BY tblCategories.CategoryID, tblCategories.CategoryName"
strSQL=strSQL & " Order By tblCategories.CategoryName ASC "
Familiar Sight
 
Join Date: Jul 2006
Posts: 181
#3: Mar 1 '09

re: combining 3 tables in a query


Thanks Dwagar, That worked great, thanks a lot
Richard
Reply