You can't do what you're trying to do using the old SQL syntax like you are
using. Try it like this:
SELECT
ans.ExamID, ans.QnAIndex, que.QtnText, que.ExamQnAType,
ans.QnAOrder, ans.QnASubOrder, ans.AnsText, ans.CorrectAns,
que.RatSurl, que.DepSurl, que.InstId, inst.InstText
FROM
ExamQuestionInfo que
LEFT OUTER JOIN ExamAnswerInfo ans on que.QnAIndex = ans.QnAIndex
AND que.ExamID = ans.ExamId
LEFT OUTER JOIN ExamInstGrpInfo inst on que.ExamID = inst.ExamId
AND que.InstId = inst.InstId
ORDER BY que.ExamID, que.QnAIndex, inst.InstId
I'm not really clear on what you're trying to do with the first two tables
though. If you are wanting all que rows whether or not there are ans rows,
then keep the left join. But, if you want only matches between these two
tables, then change the LEFT OUTER to an INNER (Outer and INNER are optional
words in most DBMS's, but since I don't know which DBMS you're using, I
can't guarantee it is in your DBMS, so I spelled it all out).
Also, if a match does not exist, you might physically get NULL printed in
the column where no match exists. If you don't want the word NULL in your
result set, use a CASE statement (if your DBMS has one) to put another word,
or a blank space in that column.
And, (Just in case you're not familiar with ANSI JOIN Syntax) if you want to
add a where clause, it goes between the last join condition and the ORDER BY
clause.
If this doesn't work, reply back to me, tell me what DBMS you're using, and
what is not working, and I'm sure we can get the results you need.
Hope this helps,
Steve Howard
-----Original Message-----
From: Michelle Kobza-Road Runner [mailto:[EMAIL PROTECTED]]
Sent: Friday, April 20, 2001 5:27 PM
To: [EMAIL PROTECTED]
Subject: joining > 2 tables with differing # of rows while using binding
columns
Hi,
I am trying to pull date from 3 different tables to generate a specifically
formatted text file. I can successfully join two tables using the ANSI LEFT
JOIN, but when I try to add a third this does not work. I have than moved to
using the older perl script " from a,b,c" . This seems to join the data but
the data from the third table is out of order or not placed correctly.
Some background:
Sun Solaris server, SQL 7.5
One database, three tables with following primary fields I am cross
referencing. The first two tables have the same number of rows with primary
keys ExamID and QnAIndex. The third table does not have the same number of
rows and only holds data that has an InstID. Primary keys to match are
ExamID and InstID
Table 1:
ExamQuestionInfo
ExamID
QnAIndex
InstID
Table 2:
ExamID
QnAIndex
Table 3:
Exam ID
InstID
When I run my script if an Exam ID does not exist in Table 3 that row is
not printed. Plus, the InstId is being place on rows that do not have an
instruction. My goal is to print all the rows from Table 1 and Table 2 even
if that do not have an InstID associated with the ExamId. My script
currently is as follows:
#statement handle to query table data
my $sth2 = $dbh->prepare(qq{ SELECT ans.ExamID, ans.QnAIndex, que.QtnText,
que.ExamQnAType, ans.QnAOrder, ans.QnASubOrder, ans.AnsText, ans.CorrectAns
, que.RatSurl, que.DepSurl, que.InstId, inst.InstText FROM ExamQuestionInfo
que, ExamAnswerInfo ans, ExamInstGrpInfo inst WHERE que.ExamID = ans.ExamId
AND que.ExamID = inst.ExamId AND que.QnAIndex = ans.QnAIndex AND que.InstId
= inst.InstId ORDER BY que.ExamID, que.QnAIndex }) or
die ( "Cannot prepare statement: ", $dbh->errstr(), "\n" );
$sth2->execute() or
die ( "Cannot execute statement: ", $sth2->errstr(), "\n" );
#associate Perl variables to output columns
my $rv2 = $sth2->bind_columns(\( $ExamId, $QnAIndex, $QtnText, $ExamQnAType,
$QnAOrder, $QnASubOrder, $AnsText, $CorrectAns, $RatSurl, $DepSurl, $InstId,
$InstText));
Any suggestions, advice very much appreciated.
Michelle