If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > Basic Problem with Table Joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-18-07, 09:08
turn68 turn68 is offline
Registered User
 
Join Date: Oct 2007
Posts: 1
Basic Problem with Table Joins

Hi All,

I've got two tables: programs and agency. On one ASP page I've listed all the agencies, which are linked to a details page by passing the agency.agency_ID variable.

On the details page, the SQL statement is failing. What I want to do is list all programs associated the agency the user clicked on by passing the agency_ID variable

Here is the SQL I'm using that isn't working:

SELECT mcaa.agency.agency_ID, mcaa.agency.agencyName,mcaa.programs.program_ID,mc aa.programs.programName,mcaa.programs.agencyID,mca a.programs.programBrief,mcaa.programs.programGroup ,mcaa.programs.directURL
FROM mcaa.programs,mcaa.agency
WHERE MMColParam = mcaa.agency.agency_ID AND mcaa.agency.agency_ID = mcaa.programs.agencyID
ORDER BY programName ASC

I really just want to display just those records where the agency_ID passed equals the agencyID.

Any thoughts?
Reply With Quote
  #2 (permalink)  
Old 10-22-07, 08:10
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
The thread title uses the word "Joins" but that SQL statement doesn't contain any (which won't help the matter!)

Let us re-write this
Code:
SELECT a.agency_ID
     , a.agencyName
     , p.program_ID
     , p.programName
     , p.agencyID
     , p.programBrief
     , p.programGroup
     , p.directURL
FROM   mcaa.programs As [p]
 INNER
  JOIN mcaa.agency As [a]
    ON a.agency_ID = p.agencyID
WHERE  a.agency_ID = MMColParam
ORDER
    BY programName ASC
The only thing left to do is replace the bit highlighted above in orange with the selected agency_id. This will all depend on how you're accessing your data and storing/accessing the variable
Perhaps a good suggestion would be to build the query up dynamically like so:-
Code:
<%
  Dim sql

  sql = ""
  sql = sql & " SELECT a.agency_ID, a.agencyName, p.program_ID, p.programName"
  sql = sql & " , p.agencyID, p.programBrief, p.programGroup, p.directURL"
  sql = sql & " FROM mcaa.programs As [p]"
  sql = sql & " INNER JOIN mcaa.agency As [a]"
  sql = sql & " ON a.agency_ID = p.agencyID"
  sql = sql & " WHERE a.agency_ID = "
  sql = sql & Request.QueryString("agency_id")
  sql = sql & " ORDER BY programName ASC"

  Response.Write(sql)
%>
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 10-22-07, 08:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by georgev
The thread title uses the word "Joins" but that SQL statement doesn't contain any
does too

it uses an inner join

it does not happen to use JOIN syntax, but it does have a join

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 10-22-07, 08:47
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quite correct actually!

However; the query written by the OP and the one by me will produce the same results but they will perform differently (JOIN before WHERE (?)).

Rudy, did I mention that I love the fact you're so finickity
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 10-22-07, 08:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by georgev
Rudy, did I mention that I love the fact you're so finickity
thank you, but i am not as "finickity" as a machine

and they will not perform differently

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 10-22-07, 09:23
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Aha! You are quite right, I'll eat my words on that one!
After some analysis I can't find a single thing different in the execution.

+1up to Rudy
__________________
George
Twitter | Blog
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On