MySQL 具有内连接的未知列

MySQL Unknown Coloumn with Inner join

这是我第一次尝试使用内部联接,我对 where 子句有疑问。 'where clause' 中的未知列 'forumtype' 但是,如果我删除那里的 where 子句,那么我将得到与 ON 子句相同的错误。 论坛 table 的 ID 等于评论 table 的 TOPIC_ID。 如果您需要任何进一步的信息,请告诉我。

$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());

您正在为列名使用别名,并试图在连接子句和 where 条件中使用它们,这是不允许的,您需要将列名与 table 别名一起使用

"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"