简体   繁体   English

如何计算每一行的连接中的所有实例?

[英]How to count all instance in the join for each row?

I have the following scheme: 我有以下方案:

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');

I use the following query: 我使用以下查询:

   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

The problem is with the COUNT(answers.username . I want to count the answers for each question but the query displays the count as 1. For example the question ID 4 is answered 3 times but the COUNT(answers.username) displays it as 1 instead of 3. 问题在于COUNT(answers.username 。我想计算每个问题的答案,但查询显示计数为1.例如,问题ID 4被回答3次,但COUNT(answers.username)显示为1而不是3。

This is the expected result: 这是预期的结果:

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

This is the result I actually get: 这是我实际获得的结果:

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

Thanks 谢谢

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

To get the correct count you should only group by the question id, but not the username: 要获得正确的计数,您应该只按问题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

If you have to get the usernames in the same query then use a join: 如果必须在同一查询中获取用户名,请使用连接:

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 sqlfiddle

or GROUP_CONCAT : 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 sqlfiddle

Or a correlated subquery: 或相关子查询:

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 sqlfiddle

EDIT 编辑

I think you looking for this , it will show all questions id and count all answers , and show all usernames. 我认为你在寻找这个,它会显示所有问题ID并计算所有答案,并显示所有用户名。

DEMO SLQFIDDLE DEMO SLQFIDDLE

How about this: 这个怎么样:

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 小提琴

As far as I see you should change this: 据我所知,你应该改变这个:

COUNT(answers.username) AS count_everyone,

to: 至:

COUNT(answers.id) AS count_everyone,

as you actually want to count the number of answers with the same ID... 因为你实际上想要计算具有相同ID的答案数...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM