2013-07-24 79 views
0

我試圖根據未回答的問題獲得總計數和新計數。mysql基於時間戳計數?

我有兩個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 

有沒有辦法給兩個語句結合返回的總問題的計數和對新問題的計數?基本上沒有答案的任何問題的計數?

回答

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 
+0

,做的伎倆...謝謝! – Paul