I am the world's newest convert to the joys of LEFT JOIN. The following is working beautifully:
$STH = $DBH->prepare("
SELECT cc.cat_subject, cc.cat_major, cc.cat_minor, cc.cat_name, c.cnum, c.unmod
FROM comment_category cc
LEFT JOIN comment c
ON cc.cat_subject = c.cat_subject AND cc.cat_major = c.cat_major AND cc.cat_minor = c.cat_minor
ORDER BY cc.cat_subject, cc.cat_major, cc.cat_minor");
What I want to do now, though, is reference another table favourite (f)
which only has a common field cnum
with comment (c)
and none with comment_category (cc)
.
I tried the following:
$STH = $DBH->prepare("
SELECT cc.cat_subject, cc.cat_major, cc.cat_minor, cc.cat_name, c.cnum, c.unmod, f.favourite
FROM comment_category cc
LEFT JOIN comment c
ON cc.cat_subject=c.cat_subject AND cc.cat_major=c.cat_major AND cc.cat_minor=c.cat_minor
LEFT JOIN favourite f
ON c.cnum=f.cnum
ORDER BY cc.cat_subject, cc.cat_major, cc.cat_minor");
It doesn't work. I'm guessing (from what I've read) that this is because I'm actually requesting another join to comment_category (cc)
rather than to comment (c)
.
In short: how do I select from A, LEFT JOIN to B, but then use a field in B to access a value from C?
UPDATE (due to punter displeasure):
The tables are:
comment_category
----------------
subject (int)
cat_major (int)
cat_minor (int)
name (varchar)
comment
-------
cnum (int)
subject (int)
cat_major (int)
cat_minor (int)
unmod (varchar)
favourite
---------
cnum (int)
... I'll stop here - it crashes because there's no favourite field in the favourite
table.
My mistake. Sorry, all.