MYSQL计数不正确

MYSQL计数不正确,mysql,sql,count,Mysql,Sql,Count,此SQL查询输出(计数的问题) 实际价值 +-----------+-----+-----------+-----------------+--------------+-------------------+ | cID | ID | readCount | UserCount | downloadCount| userDownloadCount | +-----------+-----+-----------+-----------------+----------

此SQL查询输出(计数的问题)

实际价值

+-----------+-----+-----------+-----------------+--------------+-------------------+
| cID       | ID  | readCount | UserCount       | downloadCount| userDownloadCount |
+-----------+-----+-----------+-----------------+--------------+-------------------+
|      1011 | 278 |      3168 |              67 |         3168 |                19 |
|      1011 | 272 |      9918 |             122 |         9918 |                41 |
|      1011 | 241 |     31694 |              99 |        31694 |                38 |
+-----------+-----+-----------+-----------------+--------------+-------------------+
3 rows in set
book_事件(表)

我需要将计数分为两列。readCount和downloadCount列数不正确,但UserCount、userDownloadCount列数值正确


如何解决此问题?

这是因为您在同一本书上有多个读取和下载事件,因此您的查询生成了事件的叉积

解决此问题的一个好方法是分别聚合信息片段。但是,您的查询提供了一个更简单的解决方案。只需加入book_事件表一次,然后计算不同的事件

+-----+--------+----------+--------------+
| ID  | userID | userRead | userDownload |
+-----+--------+----------+--------------+
| 278 |   5169 | 1        | 0            |
| 278 |   5169 | 0        | 1            |
| ... |   .... | .        | .            |
| 278 |   5628 | 1        | 0            |
| 278 |   5162 | 1        | 0            |
+-----+--------+----------+--------------+

我在GROUPBY子句中添加了b.cid。在GROUPBY中的SELECT子句中包含所有非聚合值是一种很好的形式。其他数据库强制执行这一点,规则是标准SQL。

我认为您的查询在技术上是错误的(按语法分组),但它与您的问题无关,可以在MySQL上运行

计数问题最常见的原因是没有正确地计算空值,但我认为这也不是您的问题

试试这个

SELECT b.cID, b.ID,
       sum(be.bookRead) as readCount,
       count(distinct case when be.bookRead = 1 then be.userId end) as UserCount,
       sum(be.userDownload) as downloadCount,
       count(distinct case when be.userDownload = 1 then be.userId end) as userDownloadCount
FROM book AS b INNER JOIN
     book_event be
     on be.bookID = s.ID
WHERE b.cID = 1011
GROUP BY b.ID, b.cid
ORDER BY b.ID DESC
+-----+--------+----------+--------------+
| ID  | userID | userRead | userDownload |
+-----+--------+----------+--------------+
| 278 |   5169 | 1        | 0            |
| 278 |   5169 | 0        | 1            |
| ... |   .... | .        | .            |
| 278 |   5628 | 1        | 0            |
| 278 |   5162 | 1        | 0            |
+-----+--------+----------+--------------+
SELECT b.cID, b.ID,
       sum(be.bookRead) as readCount,
       count(distinct case when be.bookRead = 1 then be.userId end) as UserCount,
       sum(be.userDownload) as downloadCount,
       count(distinct case when be.userDownload = 1 then be.userId end) as userDownloadCount
FROM book AS b INNER JOIN
     book_event be
     on be.bookID = s.ID
WHERE b.cID = 1011
GROUP BY b.ID, b.cid
ORDER BY b.ID DESC
SELECT 
b.cID, 
b.ID, 
sum(distinct coalesce(r.userRead,0)) AS readCount, 
count(DISTINCT r.userID) AS UserCount, 
sum(distinct coalesce(c.userDownload,0)) AS downloadCount, 
count(DISTINCT c.userID) AS userDownloadCount 
FROM 
 book AS b 
  left JOIN book_event AS r ON r.bookID=s.ID AND r.bookRead = 1 
  left JOIN book_event as c ON c.bookID=s.ID AND c.bookDownload = 1 
WHERE 
 b.cID = 1011 
GROUP BY 
 b.cID, b.ID 
ORDER BY 
 b.ID DESC