与PK和Fk关系一样。
我有两个表,一个是让我们说items_info
,第二个是poll_report
items_info 结构如下
_id Autogenerated integer based PK
Question varchar(255)
poll_report
_id Autogenerated integer based PK
questionId FK to prev table's _id
answerId integer
deviceId UNIQUE
我想使用单个查询获取以下类型的数据。
items_info._id | items_info.Question | count(poll_report.answerId) where poll_report.answerId == 1 | count(poll_report.answerId) where poll_report.answerId == 2
你能帮帮我吗?
答案 0 :(得分:1)
你可能想要这样的东西:
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;
无论如何,关键是你可以这样做:
sum(poll_report.answerId = 1)