简体   繁体   English

MySQL从表1中选择ID并从表2中选择计数(ID)

[英]Mysql select id from table1 and select count(id) from table2

I have two tables. 我有两张桌子。 I want to select id from table 1 and count the same from table2 我想从表1中选择ID,并从表2中进行计数

Table 1 表格1

Id  qId opt
1   30  Chris Christie
2   30  Hillary Clinton
3   30  Allan West
4   30  Joe Biden
5   31  Mark
6   31  Ben Johnson

Table2 表2

poll_id qId ansId
201     30  1
202     30  2
204     31  8

The below query i tried, outputs only the ansId 1 and 2 since there is no 3 and 4 in Table2. 我尝试了以下查询,因为表2中没有3和4,所以仅输出ansId 1和2。

SELECT a.Id, 
a.opt, 
COUNT(b.ansId)  
from Table1 a 
INNER JOIN Table2 b ON a.Id = b.ansId 
where a.qId =30

But i need all ansId 1,2,3,4 with count of 3 and 4 as 0 as given below. 但是我需要所有ansId 1,2,3,4,其3和4的计数为0,如下所示。

Id   opt               COUNT(b.ansId)
1    Chris Christie    1
2    Hillary Clinton   1
3    Allan West        0
4    Joe Biden         0

First thing your are missing with group by ,count is an aggregate function and this needs to be grouped,second you need to use left join with an additional condition in on clause ie and a.qId =30 so it will stil gives you the result if left id is not found in right table,using where clause will filter out the whole resultset while if you use additional condition in join this will only filter the records from the right table 首先,group by缺少您的内容,count是一个聚合函数,需要将其分组,其次,您需要在on子句(即and a.qId =30使用带有附加条件的左and a.qId =30因此它将stil给您结果如果在右表中未找到左ID,则使用where子句将筛选出整个结果集,而如果在联接中使用其他条件,则将仅从右表中筛选记录

SELECT a.Id,
a.opt,
COUNT(b.ansId)  from Table1 a
LEFT JOIN Table2 b ON a.Id = b.ansId and  a.qId =30
GROUP BY a.Id

Fiddle Demo 小提琴演示

Edit after sample dataset is updated 样本数据集更新后进行编辑

SELECT a.Id,
a.opt,
COUNT(b.ansId)  from Table1 a
LEFT JOIN Table2 b ON a.Id = b.ansId
WHERE  a.qId =30
GROUP BY a.Id

Fiddle demo 2 小提琴演示2

暂无
暂无

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

相关问题 MySQL 5.5从table2中为表1中的每个唯一ID选择MAX - MySQL 5.5 select MAX from table2 for every unique id in table1 SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause 从表1创建表3 AS SELECT WHERE ID =从表2创建ID - Create Table3 AS SELECT WHERE id from table1 = id from table2 PHP / MySQL-尝试选择两列,其中table1中的id等于table2中的id时出现问题 - PHP/MySQL - Trouble when trying to select two columns where id from table1 is equal to id from table2 如何 select 来自 table1 的记录,其中 table1.id 存在于 table2 的 id 列中? - How to select the records from a table1 where table1.id exists in id column of table2? 从表1,表2中选择*,其中表1.ID = 1,显示具有其他ID的值 - select * from table1, table2 where table1.id = 1 showing values with other id MYSQL:来自table1的SELECT 1,其中Id = 1 vs SELECT EXISTS(来自table1的SELECT 1,其中Id = 1) - MYSQL: SELECT 1 from table1 where Id = 1 vs SELECT EXISTS(SELECT 1 from table1 where Id = 1) MySQL SELECT * FROM table1,table2,table3 - MySQL SELECT * FROM table1,table2,table3 在 mysql 中选择 table1 和 table2 中的所有结果,其中两个表都有一个公共 ID - Select all results from table1 as well table2 in mysql where both table has one common id Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能 - Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM