我试图在查询中获得一对多关系的计数。
我的数据类:
data class CustomerWithCounts(
@Embedded val customer: Customer,
@Embedded val address: Address,
val orderCount: Int,
val paymentCount: Int
)
我在努力想怎么才能得到计数。
我的当前查询:
SELECT *,
COUNT(SELECT * FROM tblOrder WHERE customerId = c.id) AS 'orderCount',
COUNT(SELECT * FROM tblPayment WHERE customerId = c.id) AS 'paymentCount'
FROM tblCustomer c
LEFT JOIN tblAddress a ON c.customerBillingAddressId = a.addressId
ORDER BY c.customerFirstName, c.customerLastName
我该如何做到这一点?
1条答案
按热度按时间but5z9lq1#
假设您有两个具有一对多相关性的数据表-“Table 1”和“Table 2”,您可以使用下列SQLite查询来取得一对多相关性的计数: