简体   繁体   English

SQL Server ISNULL不适用于我的查询

[英]sql server ISNULL not working for my query

This is my second question about this problem. 这是我关于这个问题的第二个问题。 I would like get sum of column roll_sum script: 我想获得roll_sum列脚本的总和:

($sum_number + (SELECT SUM(roll_sum) FROM table_name))

not work because collumn ROLL_SUM is NULL. 不起作用,因为列ROLL_SUM为NULL。 But if try use replacement: 但是,如果尝试使用替换:

($sum_number + (SELECT SUM(ISNULL(roll_sum, 0)) FROM table_name))

not work aswell. 也不行。 But second script should replace NULL to 0? 但是第二个脚本应该将NULL替换为0吗?

SUM()交换ISNULL() SUM()

SELECT isnull(SUM(roll_sum), 0) FROM table_name;

I think there is no problem in your sql query but, you are calling sql query in php statement directly. 我认为您的sql查询没有问题,但是,您直接在php语句中调用sql查询。 The below is sample code. 下面是示例代码。 please refer to it. 请参考它。

$db = mysql_connect("hostname", "username", "password");
mysql_select_db("dbname", $db) or die("connection failed");

$query = mysql_query("select sum(roll_sum) as sum from table_name", $db);
$query_row = mysql_fetch_array($query);

// to do something you want
$value = $sum_number + $query_row["sum"];

mysql_close($db);

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

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