简体   繁体   English

复杂的SQL查询-我该怎么做?

[英]Complicated SQL query - how do i do it?

I'm currently working on a small feature for a project and wondered how best to achieve the result, i have a table full of reviews with the score being out of 5 and being stored as score in the database, on one page i want to show how many of each score reviews there are, ie number of 5 star reviews, 4 star etc 我目前正在为一个项目开发一个小功能,想知道如何最好地实现结果,我有一张充满评论的表,得分超过5分并作为得分存储在数据库中,我想在一页上显示每个分数评论中有多少,即5星评论,4星等的数量

But i don't know how best to achieve surely i don't need 5 different queries, that would be awful design would it not ? 但是我不知道如何最好地确定我不需要5个不同的查询,那将是糟糕的设计,不是吗?

Thanks and hope you can help ! 谢谢,希望能对您有所帮助!

Since I do not have your table structure, I would do something similar to this (with appropriate names replaced) 由于我没有您的表结构,因此我将执行类似的操作(替换为适当的名称)

edited SQL based on comments 根据注释编辑SQL

Select Score, COUNT (*) as NumScores
From MyTableOfScores
Group By Score
Order by Score Desc

您需要这样的东西:

select score, count(*) from reviews group by score

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM