MySQL COUNT() GROUP BY不起作用

时间:2020-12-28 22:31:19

I have a problem in counting by MySQL in a GROUP BY This is the query that does not return the desired result.

我有一个问题,用一个组中的MySQL计数,这个查询不会返回想要的结果。

SELECT COUNT(bagno)
FROM disposizione_assegnazione_pezze
JOIN pezze 
  ON pezza = id
WHERE id_prodotto_tessuto = 12096
  AND id_collezione = 11 
  AND id_stagione = 22 
  AND id_tema = 1
GROUP BY bagno

The result of the count is 3

计数的结果是3

This is the pezza table and its primary key is id

这是pezza表,其主键是id

MySQL COUNT() GROUP BY不起作用

This is the table disposizione_assegnazione_pezze that has the pezza column which refers to the previous table

这是一个名为dispose sizione_assegnazione_pezze的表,该表具有引用前一个表的pezza列

MySQL COUNT() GROUP BY不起作用

Why does not return 1 as a result my query?

为什么不返回1作为结果我的查询?


Question of the problem

I want to count how many different bagno are there

我想数一数有多少个不同的袋子

1 个解决方案

#1


4  

I dont think you need GROUP BY, instead use DISTINCT

我认为你不需要分组,而是使用不同的。

 SELECT COUNT(DISTINCT bagno)

SQL DEMO

SQL演示

Check your query without agregatted function COUNT/GROUP BY

在没有agregatted函数COUNT/GROUP BY的情况下检查查询

MySQL COUNT() GROUP BY不起作用

As you can see bagno = 55 appear three times, that is why when you group by bagno and count get 3.

你可以看到bagno = 55出现三次,这就是为什么当你分组时,bagno和count得到3。

#1


4  

I dont think you need GROUP BY, instead use DISTINCT

我认为你不需要分组,而是使用不同的。

 SELECT COUNT(DISTINCT bagno)

SQL DEMO

SQL演示

Check your query without agregatted function COUNT/GROUP BY

在没有agregatted函数COUNT/GROUP BY的情况下检查查询

MySQL COUNT() GROUP BY不起作用

As you can see bagno = 55 appear three times, that is why when you group by bagno and count get 3.

你可以看到bagno = 55出现三次,这就是为什么当你分组时,bagno和count得到3。