从两个表中选择并计算第三个表

时间:2015-07-19 17:18:05

标签: mysql sql database mysql-workbench

我有一个用户,问题和答案表,我想做的是根据问题和用户表选择他们的用户名,然后根据问题之间的关系计算答案表中的行数和答案表。请记住表格的当前状态是:

  

问题表有四列(question_id,topic_id,用户名,问题)   答案表有两列(question_id,answer)   Users表有两列(username,user_mail)   我试过的查询

SELECT 
questions.question_id,
questions.username,
questions.question,
userlog.user_mail,
COUNT(answers.answer) as answerCount
FROM
questions
LEFT JOIN answers ON answers.question_id = questions.question_id, 
userlog
WHERE
questions.topic_id = '0d3fb89c012b5af12e1e0'
    AND userlog.username = questions.username

上面的问题是,它只返回一行而不是数据库中的三行。

3 个答案:

答案 0 :(得分:2)

您的查询存在一些问题。首先,$revbyMonth = $conn->prepare("SELECT EXTRACT(MONTH FROM date) as month, EXTRACT(YEAR FROM date) as year, SUM(rev) as total FROM {$tempName} GROUP BY year, month"); 的存在使查询成为聚合查询。如果没有COUNT(),则聚合查询不能生成多行。

另一个问题:你和USERLOG有一些JOIN混淆。希望每个用户在USERLOG中只有一行,或者您最终可能会重复计算答案。

尝试此查询

GROUP BY

这应该产生你需要的多行结果集。

答案 1 :(得分:1)

试试这个:

SELECT questions.question_id,  
               questions.username, 
               questions.question, 
               userlog.user_mail, 
               (Select COUNT(answers.answer) where answers.question_id = questions.question_id) as answerCount 
FROM questions 
INNER JOIN userlog ON userlog.username = questions.username
WHERE questions.topic_id = '0d3fb89c012b5af12e1e0'

答案 2 :(得分:1)

create table users
(
    userId int auto_increment primary key,
    username varchar(100) not null,
    email varchar (100) not null
);

create table questions
(
    qId int auto_increment primary key,
    topicId varchar(50) not null,
    userId int not null,
    question varchar(1000) not null
);

create table answers
(   
    aId int auto_increment primary key,
    qId int not null,
    answer varchar(1000) not null
);

insert users (username,email) values ('sparky','sp@me.com'),('sarah','sarah@me.com');

truncate table questions;   -- for debugging
insert questions (topicId,userId,question) values ('0d3fb89c012b5af12e1e0',1,'Does life exist outside our galaxy?');
insert questions (topicId,userId,question) values ('0d3fb89c012b5af12e1e0',1,'Are fish really that dumb? Really?');
insert questions (topicId,userId,question) values ('xxxxxx',1,'Am I here?');
insert questions (topicId,userId,question) values ('xxxxxx',1,'Am you here?');

truncate table answers; -- for debugging
insert answers (qId,answer) values (1,'I hope so.');
insert answers (qId,answer) values (1,'I think so.');
insert answers (qId,answer) values (2,'What is wrong with you.');
insert answers (qId,answer) values (2,'Fish are nice.');
insert answers (qId,answer) values (2,'I like turtles.');
insert answers (qId,answer) values (3,'I like turtles too.');
insert answers (qId,answer) values (3,'Me 3.');

-- select * from users;
-- select * from questions;
-- select * from answers;


select 
q.qId,u.username,q.question,u.email,count(a.aId) as AnswerCount
from users u
join questions q
on q.userId=u.userId  and q.topicId='0d3fb89c012b5af12e1e0'
join answers a
on a.qId=q.qId
group by q.qId,u.username,q.question,u.email


+-----+----------+-------------------------------------+-----------+-------------+
| qId | username | question                            | email     | AnswerCount |
+-----+----------+-------------------------------------+-----------+-------------+
|   1 | sparky   | Does life exist outside our galaxy? | sp@me.com |           2 |
|   2 | sparky   | Are fish really that dumb? Really?  | sp@me.com |           3 |
+-----+----------+-------------------------------------+-----------+-------------+
2 rows in set (0.04 sec)