MySQL 在询问某些值和空值之间的差异时返回 null
mysql returns null when asking for difference between some value and null
在我的表格中,我有 3 列id, somevalue (float), current timestamp
下面的代码搜索今天日期的最新值,并用同周星期一的值减去该值。 但是我没有为本周的星期一存储任何值,所以目前为空。 但是以下代码的结果应该是一些不为空的值,????不明白这怎么可能。 请解释一下。
select(SELECT power FROM newdb.newmeter
where date(dt)=curdate() order by dt desc limit 1)-
(select Power from newdb.newmeter
where date(dt)=(select date(subdate(now(), interval weekday(now()) day))));
当我阅读类似的问题答案时,看起来您在 MySQL 中使用 null 所做的任何事情都是空的,这是真的吗?如果是,我该如何解决这个问题
更新:我试过这个但没有用选择总和(金额) - 合并(总和(到期),0)
只是想为此添加更多内容我正在为 C++ 中的 MySQL 调用 queryDB,如下所示
bool Querydb(char *query, double Myarray[1024])
{
//snip//
if (mysql_query(conn, query)) {
fprintf(stderr, "%sn", mysql_error(conn));
return 0;
}
else {
res = mysql_use_result(conn);
//output table name
//printf("MySQL Tables in mysql database:n");
//checking for null value in database
while((row = mysql_fetch_row(res))==NULL){
printf("ERROR_____NULL VALUE IN DATABASE ");
return 0;
}
//if not null then ...
while ((row = mysql_fetch_row(res)) != NULL){
printf("rows fetched %sn", row[0]);
sprintf(buffer,"%s",row[0]);
value1 = atof(buffer);
Myarray[i]=value1;
//printf("Myarray in sql for daybutton = %fn",Myarray[i]);
i++;
}
i=0;
//for(i=0;i<5;i++){
// printf("mya arr in sqlfunction = %fn",Myarray[i]);}
return 1;
}
printf("if here then....where??n");
//close connection
// mysql_free_result(res);
//return 0;
}
当数据库为 null 时,上述函数适用于不同的查询,但不适用于此查询
select(SELECT power FROM newdb.newmeter
where date(dt)=curdate() order by dt desc limit 1)-
(select Power from newdb.newmeter
where date(dt)=(select date(subdate(now(), interval weekday(now()) day))));
它返回 1 甚至认为答案是空...
请参阅有关使用 NULL 值的手册。他们受到特殊 http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html
您希望它的值是多少?这并不是特别特定于MySQL - 涉及空操作数(包括比较)的操作在大多数SQL方言中具有空结果。
您可能希望使用 COALESCE() 来提供一个"默认"值,当您的实际目标值为 null 时会使用该值。
解决方法是
select(SELECT power FROM newdb.newmeter
where date(dt)=curdate() order by dt desc limit 1)-
(select COALESCE(Power,0) from newdb.newmeter
where date(dt)=(select date(subdate(now(), interval weekday(now()) day))));
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在C++中NULL和__NULL之间有什么区别
- 删除指针并将指针设置为null之间有什么区别
- if(NULL==指针)和if(指针==NULL)之间有什么区别
- 比较中NULL和零之间的差异
- 二进制计数1,介于0和NULL之间
- 字符串对象和以 null 结尾的字符数组之间的差异
- 将C样式字符串初始化为NULL与空字符串之间的区别
- 2013和GCC之间的全局函数和不明确的参数NULL与char*
- c++中if(指针)与if(指针!=NULL)之间的差异,cpplint问题
- 以null结尾的字符(0)和“^@”之间的差异
- MySQL 在询问某些值和空值之间的差异时返回 null