如果传递空字符串则设置默认值 [英] Set default value if empty string is passed
本文介绍了如果传递空字符串则设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力寻找实现以下结果的解决方案.
I am trying to find a solution to achieve the following result.
如果 MySQL 查询有空字符串 ''
,则使用表架构中定义的默认值.
If MySQL query has empty string ''
, than use default value defined in a table schema.
例如
INSERT INTO `users`(`name`,`nickname`,`balance`)
VALUES ('hello','world','');
在这种情况下,如果余额列的默认值是 0
那么它应该被设置而不是空字符串.
In this case if the balance column's default value is 0
then it should be set instead of the empty string.
有可能实现吗?
推荐答案
使用变量(例如 $balance)而不是 ''(我希望您无论如何都会这样做).
use a variable (such as $balance) instead of '' (which I expect you'd be doing anyway).
在您查询之前的那一行:
On the line before your query:
if(strlen(trim($balance))==0){ $balance='0'; }
您也可以使用 mysql case 语法,这有点笨拙,但非常适合您的描述.
You could also use the mysql case syntax, which is a bit clunky, but very much on point for your description.
这篇关于如果传递空字符串则设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文