作者:阿加芬散阿加芬散 | 来源:互联网 | 2023-05-17 20:04
Iamusingsqlite3onAndroid.Iamtryingtofindthenumberofrowswhicharerelatedtoeachrow
I am using sqlite3 on Android. I am trying to find the number of rows which are related to each row in the result. The following reflects the general organization of the tables, but the details are more complex:
我在Android上使用sqlite3。我试图找到与结果中每行相关的行数。以下内容反映了表的一般组织,但细节更复杂:
dept:
+----+-----------------+
| id | name |
+----+-----------------+
employee:
+----+-----------------+-----------------+
| id | dept_id | name |
+----+-----------------+-----------------+
manager:
+----+-----------------+-----------------+
| id | dept_id | name |
+----+-----------------+-----------------+
I am trying to query all of the departments, and with each one display the count of the 1) employees and 2) managers. So the result might look something like:
我正在尝试查询所有部门,并且每个部门都显示1)员工和2)经理的计数。所以结果可能如下所示:
id | name | num_employees | num_managers
1 | IBM | 10000 | 800
2 | FB | 8000 | 20
I would like to do this all in one query so that I can use a single CursorLoader for this. I've tried this, but it's not right:
我想在一个查询中执行所有操作,以便我可以使用单个CursorLoader。我试过了,但这不对:
SELECT d.id, d.name, COUNT(e.id), COUNT(m.id)
FROM dept d
INNER JOIN employee e ON d.id = e.dept_id
INNER JOIN manager m ON d.id = m.dept_id
GROUP BY d.id;
Thanks in advance for your help!
在此先感谢您的帮助!
2 个解决方案