简体   繁体   中英

DISTINCT with GROUP BY in MySQL

If I'm selecting distinct values, but then I group the results, are the values only distinct within each grouping, or across all groupings?

For example:

month | id
      |
  01  | 17 
  01  | 17 
  01  | 19
  04  | 17
  04  | 20

If I run

select month, count(distinct id)
from table
group by month

What counts will I get for the two months?

You would get 2 for each month.

Here is a SQL Fiddle showing the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM