Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Need return a count of unique combinations of cols

Tags:

mysql

I'm coding a video game and using MySQL to display an icon showing the player where treasure is located. The database stores the locations by their X and Y on a square map. I would like to also count the number of items on each square.

So given a table such as this

Id   x   y
==  ==  ==
1    2   3
2    3   2
3    3   2
4    4   4
5    4   4
6    4   4

I would like to return something to the effect of

x y count
= = =====
4 4 3
3 2 2
2 3 1
like image 529
Shawn Avatar asked Jun 26 '12 00:06

Shawn


1 Answers

Use GROUP BY clause for results.

Select x, y, count(*) as 'count' from mytable group by x, y
like image 161
Ravinder Reddy Avatar answered Nov 03 '22 00:11

Ravinder Reddy