我有一个带有问题和答案的数据库(用户表以及以下查询的参考)表。我目前正在进行查询:
SELECT questions.*,COUNT(answers.questionid) as num_answers,users.username
FROM questions LEFT JOIN answers ON questions.questionid = answers.questionid
LEFT JOIN users ON questions.userid = users.userid
WHERE questions.pending=''
GROUP BY questions.questionid
以上查询可以解答我的问题详情,并计算了答案的数量。但是,我想在answer.pending =''中包含答案,这样当我得到答案的数量时,它只会显示已经批准的答案。
我需要在查询中添加什么才能使其正常工作?
答案 0 :(得分:0)
根据您的问题,您需要答案。支持=''
SELECT questions.*,COUNT(answers.questionid) as num_answers,users.username
FROM questions LEFT JOIN answers ON questions.questionid = answers.questionid
LEFT JOIN users ON questions.userid = users.userid
WHERE questions.pending='' and answers.pending=''
GROUP BY questions.questionid
使用上面的查询
答案 1 :(得分:0)
请尝试使用此代码,我认为你应该使用null而不是'',
SELECT questions.*,COUNT(answers.questionid) as num_answers,users.username
FROM questions LEFT JOIN answers ON questions.questionid = answers.questionid
LEFT JOIN users ON questions.userid = users.userid
WHERE questions.pending IS NULL and answers.pending IS NULL
GROUP BY questions.questionid