-1

I have query about Group By in Mysql

Looks like my table col1 and col2

1 1
4 6
5 4
2 3
1 9
2 1
3 2

Need to display the output like this

col1&col2 and count(col1&col2)

1   4
2   3
3   2
4   2
5   1
6   1
9   1

can any one suggest to get the above result as output using mysql query?

Thanks in advance

3
  • 5
    your "would like" sample makes no sense whatsoever. Commented Jan 21, 2014 at 4:52
  • You mean - count in 2 colums? Like '1' is present 4 times, so it's 4. '2' occur 3 times, so it's 3. Right? Does order important for you? Commented Jan 21, 2014 at 5:01
  • Have a look here- stackoverflow.com/questions/8574093/… Commented Jan 21, 2014 at 5:03

3 Answers 3

2
SELECT my_table.col, count(my_table.col)
FROM 
(
   SELECT col1 as col FROM your_table
   UNION ALL
   SELECT col2 as col FROM your_table
) AS my_table
GROUP BY col

Don't forget the table names and you have to have a table alias. I tried to make this a comment but didn't have enough points to comment. Just an addendum to @tigran

Sign up to request clarification or add additional context in comments.

Comments

2
SELECT some_table.col, count(some_table.col)
FROM 
(
SELECT col1 as col FROM table
UNION ALL
SELECT col2 as col FROM table
)  AS some_table
GROUP BY col

Something like this - MySQL COUNT() multiple columns

2 Comments

sqlfiddle.com/#!2/fff3d/5 fiddle for good measure, you beat me to it while I was building this
Thanks to @Gilberg, table alias is required.
0

Finally i got the solution with the help of you all

SELECT col, count(col) FROM ( SELECT col1 as col FROM table_name UNION ALL SELECT col2 as col FROM table_name ) AS temp_table GROUP BY col ORDER BY col

Thanks to all of you @ Marc B, @Tigran, @serakfalcon and @Gilberg

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.