2011-04-25 52 views
0

正如在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 

你能幫我嗎?

回答

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) 
+0

好,你[R天才,這是查詢性能明智的好?我不知道這一點。 – Abhi 2011-04-25 08:53:08