如何给条件两列?

时间:2015-02-27 09:38:27

标签: sql sql-server-2008

Table1中有三列S_Name, Books, Val

S_Name     Books   Val
-----------------------
  A         ab     2
  B         aa     3
  A         aa     1
  C         ab     2
  A         ac     3
  D         ac     3

现在我想获得一个val的总和,其中B,C学生的所有书籍都将被计算在内,而A只有一个ab,并且将计算ac书。

2 个答案:

答案 0 :(得分:0)

查询将为

SELECT 
    sum(val) 
FROM
    Table1 
WHERE 
    (S_NAME IN ('B','C')) OR (S_name = 'A' AND Books IN ('ab','ac'))

答案 1 :(得分:-1)

SELECT 
    sum(val) 
FROM
    Table1 
WHERE
    (S_NAME IN ('B','C')) OR (S_name = 'A' AND Books IN ('ab','ac'))