MySQL Unknown Coloumn with Inner join

时间:2015-05-08 19:54:33

标签: mysql inner-join

这是我第一次尝试使用内部联接,我对where子句有疑问。 'where子句'中的未知列'forumtype' 但是,如果我删除where子句,那么我将得到与ON子句相同的错误。 论坛表的ID等于评论表的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());

1 个答案:

答案 0 :(得分:3)

您正在使用列名的别名并尝试在join子句中使用它们,并且在不允许的情况下,您需要使用列名以及表别名

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