基于时间戳的 mysql 计数?

标签: sql MySQL PHP
发布时间: 2015/6/11 21:03:54
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要的总数和新的计数基于不回答问题。

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

指望用户做不回答的所有问题

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
赞助商