我有一张桌子,product
每种产品可能是delivered
、、、idle
。shipping
preparing
我想显示一个包含每个州的产品数量的列表,我可以在此处查看如何查询该列表:
然而,这个查询返回什么,以及如何将返回值分配给 4 个整数,称为deliveredCount
, idleCount
, shippingCount
, preparingCount
?
PS:郑重声明,我在 Android 和 JAVA 中使用 SQLite 和 OrmLite
编辑:在这个问题中,人们解释了当您想要获得多个计数时要执行什么查询,但他们没有告诉我们该查询返回什么以及以什么格式。例如:
SELECT a.distributor_id,
(SELECT COUNT(*) FROM myTable WHERE level='personal' and distributor_id = a.distributor_id) as PersonalCount,
(SELECT COUNT(*) FROM myTable WHERE level='exec' and distributor_id = a.distributor_id) as ExecCount,
(SELECT COUNT(*) FROM myTable WHERE distributor_id = a.distributor_id) as TotalCount
FROM myTable a ;
Run Code Online (Sandbox Code Playgroud)
它的返回类型是什么以及格式是什么?
PS2:有人很快就否决了我的问题,因为它缺乏足够的信息。然后我编辑了它,但反对票仍然存在:(
很难肯定地说,但听起来您需要使用您提供的链接中最佳答案的版本。
就像是;
SELECT ProductID,
COUNT(*) AS Total,
SUM(CASE WHEN pStatus = 'delivered' THEN 1 ELSE 0 END) DeliveredCount,
SUM(CASE WHEN pStatus = 'idle' THEN 1 ELSE 0 END) IdleCount,
SUM(CASE WHEN pStatus = 'shipping' THEN 1 ELSE 0 END) ShippingCount,
SUM(CASE WHEN pStatus = 'preparing' THEN 1 ELSE 0 END) PreparingCount
FROM ProductTable
GROUP BY ProductID
Run Code Online (Sandbox Code Playgroud)
这将返回类似的内容;
ProductID | DeliveredCount | IdleCount | ...
1 | 250 | 3250 | ...
Run Code Online (Sandbox Code Playgroud)