日期:2014-05-16  浏览次数:20884 次

有个SQL语句不会写,请大家帮帮忙。
现在我有三个表,question,class,answer
现在
 "SELECT * FROM (question LEFT JOIN class ON question.q_upclass=class.c_id) WHERE question.q_upclass IN ("&allupid&")"&sql_limit&" ORDER BY question.q_lastmodify DESC"
能够正确执行
然而我现在需要在answer表中查找出对应每个question的数目,所以我写成了这样
"SELECT q_upclass,q_title,q_id,c_id,c_lev,c_name,COUNT(a_id) AS answercount FROM (question LEFT JOIN class ON question.q_upclass=class.c_id) LEFT JOIN answer ON answer.a_upid=question.q_id WHERE question.q_upclass IN ("&allupid&")"&sql_limit&" ORDER BY question.q_lastmodify DESC"

也就是把第一个句子中的*改成了我需要的字段,然后加入了一个新的LEFT JOIN语句:LEFT JOIN answer ON answer.a_upid=question.q_id
可以执行错误。提示:

错误类型:
Microsoft JET Database Engine (0x80040E21)
试图执行的查询中不包含作为合计函数一部分的特定表达式 'q_upclass' 。

希望大家能帮帮我。

------解决方案--------------------
"SELECT q_upclass,q_title,q_id,c_id,c_lev,c_name,COUNT(a_id) AS answercount FROM (question LEFT JOIN class ON question.q_upclass=class.c_id) LEFT JOIN answer ON answer.a_upid=question.q_id WHERE question.q_upclass IN ("&allupid&")"&sql_limit&" group by q_upclass,q_title,q_id,c_id,c_lev,c_name ORDER BY question.q_lastmodify DESC"

------解决方案--------------------
SELECT q_upclass,q_title,q_id,c_id,c_lev,c_name,COUNT(a_id) AS answercount FROM (question LEFT JOIN class ON question.q_upclass=class.c_id) LEFT JOIN answer ON answer.a_upid=question.q_id WHERE question.q_upclass IN ("&allupid&")"&sql_limit&" &" group by q_upclass,q_title,q_id,c_id,c_lev,c_name ORDER BY question.q_lastmodify DESC