mysql根据时间戳计算?

时间:2013-07-24 19:16:30

标签: php mysql sql

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

我有两个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

有没有办法将这两个语句结合起来,以返回总问题数和新问题数?基本上没有回答任何问题的计数?

1 个答案:

答案 0 :(得分: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