使用SQL中的WHERE子句进行求和

huangapple go评论75阅读模式
英文:

Sum using where clause in SQL

问题

以下是翻译好的部分:

  1. 我正在尝试根据一个单独的列中的文本是否符合某些条件来对该列中的值进行求和。我正在尝试使用Where子句来实现这一目标,但一直得到0作为答案。
  2. 表格
  3. | 人员 | 分数 |
  4. | -------- | -------- |
  5. | A | 70 |
  6. | B | 85 |
  7. | C | 50 |
  8. | D | 100 |
  9. | E | 20 |
  10. 我想要得到人员 ADE 的分数列总和。我期望得到如下结果:
  11. | Sum(分数)
  12. | -------- |
  13. | 190 |
  14. 以下是我正在使用的表达式,其中总和错误地返回为0
  15. SELECT SUM (分数)
  16. FROM 表*
  17. WHERE 人员 = 'A' OR 'D' OR 'E';
英文:

I am trying to Sum the value in a certain column, based upon if the text in a separate column matches certain criteria. I am trying to use the Where clause to do this, and keep getting 0 as an answer.

Table

Person Score
A 70
B 85
C 50
D 100
E 20

I want to get a Sum of the Score Column for Persons A, D, E. I expect to get this

Sum(Score)
190

Here is the expression I am using, where the Sum is incorrectly returned as 0

  1. SELECT SUM (Score)
  2. FROM T*
  3. WHERE Person = 'A' OR 'D' OR 'E';

答案1

得分: 3

问题出在你的 where 语句,当使用 OR 时,你必须再次输入列名,所以你可以这样做:

  1. WHERE Person = 'A' OR Person = 'D' OR Person = 'E';

或者更好的方法是使用 IN

  1. WHERE Person IN ('A', 'D', 'E');
英文:

The problem is your where, when using OR you must type the column name again, so you could do:

  1. WHERE Person = 'A' OR Person = 'D' OR Person = 'E';

or do better, using IN:

  1. WHERE Person IN ('A', 'D', 'E');

答案2

得分: 0

似乎你正在寻找IN ()操作符:

  1. SELECT SUM (Score)
  2. FROM T*
  3. WHERE Person IN ('A', 'D', 'E');
英文:

Seems like your looking for the IN () operator:

  1. SELECT SUM (Score)
  2. FROM T*
  3. WHERE Person IN ('A', 'D', 'E');

答案3

得分: 0

尝试这个:

  1. SELECT person, SUM(Score)
  2. FROM T
  3. WHERE Person = 'A' OR Person = 'D' OR Person = 'E'
  4. GROUP BY
  5. person

你还可以将这些 'or' 改为上面提到的 'in' 结构。

英文:

Try this one:

  1. SELECT person, SUM (Score)
  2. FROM T
  3. WHERE Person = 'A' OR 'D' OR 'E'
  4. Group by
  5. person

You could also change this 'or's to presented above 'in' structure

huangapple
  • 本文由 发表于 2023年2月24日 00:55:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定