今天要做一个字段求和的小查询,查询一个表中,字段1,字段2和字段3之和大于0的结果,故写了如下的SQL语句,供参考。
有一点要注意的是,在Where字句中,不能有自己运算后得到的字段。
sqlstr = "SELECT W.ID,W.Weather,greenColor,(SELECT redColor+greenColor+blueColor FROM Weather N WHERE N.ID = W.ID) AS AllValue FROM Weather W WHERE (SELECT redColor+greenColor+blueColor FROM Weather N WHERE N.ID = W.ID)>0 ORDER BY ID";
即如下的SQL语句是行不通的,说是"至少一个参数没有被指定值"。以史为鉴,故此保存。
sqlstr = "SELECT W.ID,W.Weather,greenColor,(SELECT redColor+greenColor+blueColor FROM Weather N WHERE N.ID = W.ID) AS AllValue FROM Weather W WHERE AllValue >0 ORDER BY ID";