Reputation: 911
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
Upvotes: 6
Views: 6337
Reputation: 470
What about concatenate your 2 columns and use DISTINCT ?
Select x, y, count(DISTINCT CONCAT(x, '_', y)) as 'count' from mytable group by x, y
Upvotes: 6
Reputation: 24002
Use GROUP BY
clause for results.
Select x, y, count(*) as 'count' from mytable group by x, y
Upvotes: 14