0

我正在尝试根据未回答的问题获得总计数和新计数。

我有两个如下所示的 SQL 调用:

// retrieves all ids of questions and the timestamps when recorded
SELECT id, timestamp FROM `profile_questions` q WHERE q.user_id=5

// output:
id timestamp
-- ---------
1  1374677344
2  1374677514

// retrieves all answered questions
SELECT a.timestamp 
FROM `profile_answers` a 
LEFT JOIN profile_questions q ON q.id = a.question_id 
WHERE a.answered_by=5

有没有办法将这两个语句结合起来以返回总问题数和新问题数?基本上算什么问题没有回答?

4

1 回答 1

2

要计算用户未回答的所有问题,请执行

SELECT count(q.id)
FROM `profile_questions` q 
LEFT JOIN profile_answers a ON q.id = a.question_id 
                            and q.user_id = a.answered_by
WHERE q.user_id = 5
and a.answered_by IS NULL
于 2013-07-24T19:20:11.417 回答