简体   繁体   English

根据主键表的主键获取外键表的行数

[英]Getting row count of foreign key table's rows on the basis of Primary key table's Primary key

As usual in PK and Fk relationship. 照常在PK和Fk关系中。

I am having two tables one is lets say items_info and second is poll_report 我有两个表,一个是说items_info ,第二个是poll_report

items_info structure is as follows items_info结构如下

 _id       Autogenerated integer based PK

Question   varchar(255) 

poll_report poll_report

_id       Autogenerated integer based PK

questionId FK to prev table's _id

answerId  integer

deviceId  UNIQUE

I want to fetch following type of data using single query. 我想使用单个查询获取以下类型的数据。

items_info._id   |  items_info.Question  |  count(poll_report.answerId) where poll_report.answerId  == 1 |  count(poll_report.answerId) where poll_report.answerId == 2

Can you please help me ? 你能帮我么 ?

You probably want something like like this: 您可能想要这样的东西:

select items_info._id , items_info.Question , sum(poll_report.answerId =1) , sum(poll_report.answerId =2) from items_info,poll_report where items_info._id=poll_report.questionId group by items_info._id;

Anyway, the key thing is that you can do this: 无论如何,关键的是您可以执行以下操作:

sum(poll_report.answerId = 1)

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

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