从两个表中选择并计算第三个
Selecting from two tables and counting a third
我有一个用户、问题和答案 table,我想做的是 select 来自问题和用户 table 基于他们的用户名,然后计算答案中的行数 table 基于问题和答案之间的关系 table。请记住 table 的当前状态是:
Questions table has four columns (question_id, topic_id, username, question)
Answers table has two columns (question_id, answer)
Users table has two columns (username, user_mail)
The query i tried
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
上面的问题是,它return只有一行而不是数据库中的三行。
您的查询存在一些问题。首先,COUNT()
的存在使查询成为聚合查询。没有 GROUP BY
,聚合查询只能生成一行。
另一个问题:您将 JOIN 与 USERLOG 混淆了。希望每个用户在 USERLOG 中只有一行,否则您最终可能会重复计算答案。
试试这个查询
SELECT questions.question_id,
questions.username,
questions.question,
userlog.user_mail,
COUNT(answers.answer) as answerCount
FROM questions
LEFT JOIN userlog ON questions.username = userlog.username
LEFT JOIN answers ON answers.question_id = questions.question_id
WHERE questions.topic_id = '0d3fb89c012b5af12e1e0'
GROUP BY questions.question_id, questions.username, questions.question, userlog.user_mail
ORDER BY questions.username, questions.question_id
这应该会产生您需要的多行结果集。
试试这个:
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'
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)
我有一个用户、问题和答案 table,我想做的是 select 来自问题和用户 table 基于他们的用户名,然后计算答案中的行数 table 基于问题和答案之间的关系 table。请记住 table 的当前状态是:
Questions table has four columns (question_id, topic_id, username, question) Answers table has two columns (question_id, answer) Users table has two columns (username, user_mail) The query i tried
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
上面的问题是,它return只有一行而不是数据库中的三行。
您的查询存在一些问题。首先,COUNT()
的存在使查询成为聚合查询。没有 GROUP BY
,聚合查询只能生成一行。
另一个问题:您将 JOIN 与 USERLOG 混淆了。希望每个用户在 USERLOG 中只有一行,否则您最终可能会重复计算答案。
试试这个查询
SELECT questions.question_id,
questions.username,
questions.question,
userlog.user_mail,
COUNT(answers.answer) as answerCount
FROM questions
LEFT JOIN userlog ON questions.username = userlog.username
LEFT JOIN answers ON answers.question_id = questions.question_id
WHERE questions.topic_id = '0d3fb89c012b5af12e1e0'
GROUP BY questions.question_id, questions.username, questions.question, userlog.user_mail
ORDER BY questions.username, questions.question_id
这应该会产生您需要的多行结果集。
试试这个:
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'
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)