-1

This is the first time I tried to use inner join, and I have a problem with that where clause. Unknown column 'forumtype' in 'where clause' However if I delete there where clause, then I will get the same error with the ON clause. The forum table's ID is equal to the comment's table's TOPIC_ID. Please let me know if You need any further information.

$query = mysql_query("SELECT f.id AS forumid, f.class AS forumclass, 
f.date AS forumdate, f.type AS forumtype, f.name AS forumname, f.author AS forumauthor,
f.user AS forumuser, f.url AS forumurl,
c.id AS commentid, c.user_id AS commenutuserid, c.user_name AS commentusername,
c.topic_id AS commenttopic, c.date AS commentdate
FROM ".$prefix."forum AS f
INNER JOIN ".$prefix."comment AS c
ON commenttopic = forumid
WHERE forumtype=1 AND forumclass='$c' ORDER BY commentdate DESC LIMIT $offset, $limit ") or die(mysql_error());
Phoenixy
  • 104
  • 1
  • 14

1 Answers1

3

You are using alias for the column names and trying to use them in the join clause and where condition which is not allowed you need to use the column name along with the table alias name

"SELECT 
f.id AS forumid, 
f.class AS forumclass, 
f.date AS forumdate,
f.type AS forumtype, 
f.name AS forumname,
f.author AS forumauthor,
f.user AS forumuser, 
f.url AS forumurl,
c.id AS commentid, 
c.user_id AS commenutuserid, 
c.user_name AS commentusername,
c.topic_id AS commenttopic, 
c.date AS commentdate
FROM ".$prefix."forum AS f
INNER JOIN ".$prefix."comment AS c
ON c.topic_id = f.id
WHERE 
f.type=1 
AND f.class='$c' 
ORDER BY c.date DESC LIMIT $offset, $limit"
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63