Mysql group by and count rows task

let's say mysql is something like this

select x,y from xx group by y 

I want to know how many rows will be selected, I tried to use an account, but it will not return all the results, since I am using a group.

how to do it?

thanks

+7
sql mysql
source share
3 answers

You can wrap your query as follows:

 SELECT COUNT(*) FROM (select x,y from xx group by y) sub; 
+14
source share

Suppose you have a table with the content below:

  ------------------- | ID | NAME | GROUP | +-------------------+ | 1 | A | 1 | +-------------------+ | 2 | B | 2 | +-------------------+ | 3 | C | 2 | +-------------------+ | 4 | D | 3 | +-------------------+ | 5 | E | 1 | +-------------------+ | 6 | F | 3 | +-------------------+ 

The following self LEFT JOIN counts the number of different values ​​in a GROUP.

 SELECT COUNT(*) FROM table AS t1 LEFT JOIN table AS t2 ON t2.GROUP = t1.GROUP AND t2.ID > t1.ID WHERE t2.id IS NULL; 

What this query does is find out for each group the item with the highest identifier.

0
source share

You can check my annas

 SELECT sum(1) as counttotal FROM ( Your query with group by operator ) as T 
0
source share

All Articles