简体   繁体   English

MYSQL:如何将分组的SUM()的结果用作WHERE子句中的条件?

[英]MYSQL: How to use the result of a grouped SUM() as condition in WHERE clause?

In my Table of TEAMS with relational table>rows for its PLAYERS 在我的TEAMS表中,其中关系表>行数

Each Team has 3 or more players Each Player associated with the team has a points column 每个团队有3个或更多的球员与团队相关的每个球员都有一个积分栏

I need to retrieve only the TEAM w/c has accumulated player points of 300 or greater 我只需要检索w / c拥有300点或更高的累积玩家积分的TEAM

I have gone to this point so far: 到目前为止,我已经到了这一点:

在此处输入图片说明

Problem with this is it returns a result set contain ALL the rows in the TEAMs table. 问题在于它返回的结果集包含TEAMs表中的所有行。

在此处输入图片说明

How do I make it return result only if SUM is > than , say, 300? 仅当SUM>>,例如300时,如何使它返回结果?

I tried adding: 我尝试添加:

**WHERE total_points >= '300'**

but obviously that returns an "unknown column" error. 但显然会返回“未知列”错误。

Any ideas? 有任何想法吗?

PS I could easily get what i want via PHP > loop but it's such a waste on Server resource to be loading THOUSANDS of Team rows just to get a few that meets a criteria.. PS我可以很容易地通过PHP> loop得到我想要的东西,但是要装载成千上万的Team列来获取一些符合条件的文件,这在Server资源上是一种浪费。

尝试使用HAVING子句代替WHERE

HAVING total_points >= 300

Use the having clause .. it applies to groupings. 使用hading子句..适用于分组。 (Similar to where) (类似于哪里)

so after the group put having sum(players.points)>300 所以在小组把总和(players.points)> 300之后

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

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