目前我有以下查询来计算购买特定商品时具有不同(客户用户名和商店 ID)的行数。我如何计算同一查询中 item_id = 3 的总行数?
SELECT count(*)
FROM (
SELECT DISTINCT customer_username, store_id
FROM transactions
WHERE item_id = 3)
最佳答案
你可以尝试这样的事情:
SELECT COUNT(*), SUM(customer_cnt) FROM (
SELECT customer_username, store_id, COUNT(*) AS customer_cnt
FROM transactions
WHERE item_id = 3
GROUP BY customer_username, store_id
) t;
关于MySQL Count all 和 Count distinct 超过 2 列相同的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28311822/