SQL查询计算对象中每个元素的数量
我有一个像这样的数据库表:
Room Name || Animal
----------------------------
Room1 || Cat
Room1 || Dog
Room1 || Dog
Room2 || Cat
Room2 || Cat
Room2 || Dog
Room2 || Dog
我想用 SQL 查询读取这个表,结果我想计算房间里每只动物的数量:(
Room1: 1 cat 2 dog
Room2: 2 cats 2 dog
输出格式并不重要)
是否有一个 SQL 查询可以帮助?或者我应该阅读整个表格并以编程方式过滤它?
感谢您的帮助
I have a database table like this:
Room Name || Animal
----------------------------
Room1 || Cat
Room1 || Dog
Room1 || Dog
Room2 || Cat
Room2 || Cat
Room2 || Dog
Room2 || Dog
I want to read this table with a SQL query and as result I want to count the number of each animal in the room:
Room1: 1 cat 2 dog
Room2: 2 cats 2 dog
(The output format doesn't matter)
Is there a SQL query that can help? Or should I read the whole table and filter it programatically?
Thank you for any help
这将返回
This would return