2012-12-22 44 views
5

我有以下方案:如何統計每行的連接中的所有實例?

CREATE TABLE IF NOT EXISTS `answers` (
`id` bigint(20) unsigned NOT NULL, 
`answer` varchar(200) NOT NULL, 
`username` varchar(15) NOT NULL, 
`date` datetime NOT NULL, 
PRIMARY KEY (`id`,`username`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

INSERT INTO `answers` (`id`, `answer`, `username`, `date`) VALUES 
(1, 'gfdsf', 'guy', '2012-12-22 00:00:00'), 
(4, 'gfdddsfs', 'maricela', '2012-12-22 00:00:00'), 
(4, 'gfddsfs', 'mikha', '2012-12-22 00:00:00'), 
(4, 'gfdsfs', 'guy', '2012-12-22 00:00:00'); 

CREATE TABLE IF NOT EXISTS `questions` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
`asker_username` varchar(15) NOT NULL, 
`target_username` varchar(15) NOT NULL, 
`question` varchar(200) NOT NULL, 
`hide` enum('y','n') NOT NULL DEFAULT 'n', 
`date` datetime NOT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; 

INSERT INTO `questions` (`id`, `asker_username`, `target_username`, `question`, `date`) VALUES 
(1, 'mikha', 'guy', 'testo festo', '2012-12-22 00:00:00'), 
(2, 'mikha', 'guy', 'saaaaaaaar', '2012-12-22 00:00:00'), 
(3, 'sys.tem', 'every.one', 'test g1', '2012-12-06 00:00:00'), 
(4, 'sys.tem', 'every.one', 'test g2', '2012-12-06 00:00:00'); 

我用下面的查詢:

SELECT   
    questions.id AS questionid, 
    COUNT(answers.username) AS count_everyone, 
    answers.username 
    FROM questions 
    LEFT JOIN answers ON questions.id = answers.id 
    GROUP BY questions.id,answers.username 

的問題是與COUNT(answers.username。我想爲每個問題計算答案,但查詢將計數顯示爲1.例如,問題ID 4回答了3次,但COUNT(answers.username)顯示爲1而不是3.

這是預期結果:

  questionid count_everyone username 
       1   1   guy 
       2   0   null 
       3   0   null 
       4   3    guy 
       4   3   maricela 
       4   3    mikha 

這是結果我居然得到:

  questionid count_everyone username 
       1   1   guy 
       2   0   null 
       3   0   null 
       4   1    guy 
       4   1   maricela 
       4   1    mikha 

感謝

+1

既然你通過用戶名進行分組,你爲什麼會想到每行一個以上的用戶名? – Barmar

+0

如果我通過用戶名去除分組,只會顯示一個答案。我想同時顯示所有答案和他們的計數...謝謝:) –

+0

maricela只回答了問題4一次,那麼爲什麼你期望計數是3? – Barmar

回答

2

爲了得到正確的計數,你應該僅由問題ID組,但沒有用戶名:

SELECT   
    questions.id AS questionid, 
    COUNT(answers.username) AS count_everyone 
FROM questions 
LEFT JOIN answers ON questions.id = answers.id 
GROUP BY questions.id 

如果來獲取用戶名S IN相同的查詢,然後使用一個連接:

SELECT questionid, count_everyone, username 
FROM 
(
    SELECT   
     questions.id AS questionid, 
     COUNT(answers.username) AS count_everyone 
    FROM questions 
    LEFT JOIN answers ON questions.id = answers.id 
    GROUP BY questions.id 
) T1 
LEFT JOIN answers ON T1.questionid = answers.id 

sqlfiddle

GROUP_CONCAT

SELECT   
    questions.id AS questionid, 
    COUNT(answers.username) AS count_everyone, 
    GROUP_CONCAT(answers.username) AS usernames 
FROM questions 
LEFT JOIN answers ON questions.id = answers.id 
GROUP BY questions.id 

sqlfiddle

或者相關子查詢:

SELECT   
    questions.id AS questionid, 
    (SELECT COUNT(*) FROM answers WHERE questions.id = answers.id) AS count_everyone, 
    answers.username 
FROM questions 
LEFT JOIN answers ON questions.id = answers.id 

sqlfiddle

+0

+1,但我認爲的'說法count「不應該是」用戶名「。據我瞭解這個問題,回答的人是否有用戶名並不重要。 – a1ex07

+0

如果我按問題ID唯一,只有1回答將顯示:)需要 –

+0

此數只有當'questions.target_username'等於'every.one'不是所有的問題。所以你建議更多的做一個單獨的查詢,因爲它偶爾? –

0

至於我看你應該改變這樣的:

COUNT(answers.username) AS count_everyone, 

到:

COUNT(answers.id) AS count_everyone, 

,你居然要算具有相同ID答案的數目......

+0

都給予同樣的結果:)'answers.username'是唯一的也是如此非此即彼這個...感謝 –

+0

我的壞,你戶名組,這並不改變什麼:S – Theolodis

1

編輯

我想你找這個,它會顯示所有問題的ID和計數所有的答案,並顯示所有用戶名。

DEMO SLQFIDDLE

+0

如果在計劃檢查問題4號,你會發現它實際上是由3人回答:)我需要按'questions.id'因爲我想要顯示的問題,即使它沒有回答。 –

+0

檢查我的上一次編輯,然後如果你想要按組的問題ID –

+0

再次按'問題分組。ID'只會顯示所有的答案:D我想顯示所有的答案和他們的計數:) –

4
select q.id, coalesce(j.AnswerCount, 0) as AnswerCount, a.username 

from questions q 

left outer join 

(select id as Qid, count(answer) as AnswerCount 
from answers 
group by id) j 

on q.id = j.Qid 

left outer join 
answers a on q.id = a.id 
+0

子查詢我認爲大規模表現不佳。沒有子查詢有沒有另一種方法嗎?謝謝:) –

+0

如果你想要上面的答案,你必須首先做每個問題COUNT。你爲什麼不先試試,然後決定它如何擴展? – ExactaBox

+0

我在這裏有很多解決方案......我只是擔心......反正我還沒有測試......感謝您的回答:) –

1

如何:

SELECT   
    questions.id AS questionid, 
    COUNT(answers.username) AS count_everyone, 
    GROUP_CONCAT(answers.username) users 
FROM questions 
LEFT JOIN answers ON questions.id = answers.id 
WHERE questions.target_username = 'every.one' 
GROUP BY questions.id 

FIDDLE

+0

馬克已經發布了這個解決方案。我可以像這樣工作,但我需要單獨的每行不像這樣組合。我想無論如何給一個獨特的自動增量的答案,因爲我認爲這將是更好的大規模..感謝@Barmar :) –

+0

如果你真的需要每一個單獨的行,然後ExactaBox的答案是你應該使用。 – Barmar

相關問題