mysql返回empty,MySQL返回一个空字段:CONCAT(nonEmpty1,empty2,nonEmpty3)= NULL

I have PHP 5 code accessing a MyISAM table on MySQL 5 server. The query looks like this:

SELECT CONCAT(fName1,' ',mName2,' ',lName3) AS userName

FROM users

WHERE level > 10

When there's no mName filled in, I am expecting output like "fname lname" , but I'm getting "" (empty string) instead (the number of rows returned is correct). Where am I making a mistake?

PHP code:

$result = mysql_query($the_above_query);

while ($result_row = mysql_fetch_assoc($result)) {

// do stuff with the name

// except I'm getting empty strings in $result_row['userName']

}

Relevant part of table structure:

CREATE TABLE users {

/* -snip- */

`fName1` varchar(50) default NULL,

`mName2` varchar(50) default NULL,

`lName3` varchar(50) default NULL,

`level` int(11) default 0,

/* -snip- */

} ENGINE=MyISAM DEFAULT CHARSET=utf8;

(also, is this way (column concatenation in MySQL) a good idea, or should I fetch the columns to PHP and join them there?)

Turns out that I was getting back a NULL; PHP treats a returned NULL and empty string("") similarly, you'd have to compare with === to see the difference.

解决方案

[23 May 2003 4:32] Alexander Keremidarski

Thank you for taking the time to write to us, but this is not

a bug. Please double-check the documentation available at

http://www.mysql.com/documentation/ and the instructions on

how to report a bug at http://bugs.mysql.com/how-to-report.php

This is doccumented behaviour of CONCAT() function.

From Manual chapter 6.3.2 String Functions

CONCAT(str1,str2,...)

Returns the string that results from concatenating the arguments. Returns NULL if any

argument is NULL

Use CONCAT_WS() instead or wrap NULLable paremeters with IFNULL() function.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值