代码之家  ›  专栏  ›  技术社区  ›  Piskvor left the building Rohit Kumar

mysql返回空字段:concat(nonempty1,empty2,nonempty3)=null

  •  13
  • Piskvor left the building Rohit Kumar  · 技术社区  · 15 年前

    我有php 5代码访问mysql 5服务器上的myisam表。查询如下:

    SELECT CONCAT(fName1,' ',mName2,' ',lName3) AS userName 
        FROM users 
        WHERE level > 10
    

    如果没有填写姓名, 我期望输出像“fname lname”,但我得到的是“”(空字符串)。 (返回的行数正确)。我在哪里犯错?

    PHP代码:

    <?php
    $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']
    }
    

    表结构相关部分:

    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;
    

    (同样,这种方式(MySQL中的列连接)是一个好主意吗,还是应该将这些列提取到PHP并在那里连接起来?)


    结果发现我得到的是一个空值;php处理返回的空值和空值字符串(“”)类似,您必须将其与==进行比较才能看到区别。

    6 回复  |  直到 12 年前
        1
  •  25
  •   chocojosh    15 年前

    谷歌: http://bugs.mysql.com/bug.php?id=480

    [2003年5月23日4:32]亚历山大·凯雷米达斯基

    谢谢你抽出时间给我们写信,但这不是 臭虫请仔细检查以下文件: http://www.mysql.com/documentation/ 以及关于 如何报告错误 http://bugs.mysql.com/how-to-report.php

    这是concat()函数的文档化行为。

    摘自手册第6.3.2章字符串函数

    concat(str1,str2,…) 返回由连接参数产生的字符串。如果有的话返回空值 参数为空

    使用concat_ws()代替,或使用ifnull()函数包装可为空的参数。

    concat-ws的文档和用法: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws

        2
  •  16
  •   ErsatzRyan    15 年前

    来自MySQL文档

    concat()如果有任何参数,则返回空值 是空的。

    您要使用concat_ws()。

    CONCAT_WS(separator,str1,str2,...)
    

    但最好的办法是将其收回并使用PHP,因为如果以后需要不同的格式或这些字段中的一个,则必须进行另一个DB调用。

        3
  •  11
  •   Keeper    15 年前

    在MySQL中,将任何字符串连接到空值会导致空值。 在使用ifnull连接之前,必须检查是否为空:

    SELECT CONCAT(IFNULL(fName1,''),' ',IFNULL(mName2,''),' ',IFNULL(lName3,'')) AS userName 
    FROM users 
    WHERE level > 10
    
        4
  •  3
  •   ImaginedDesign    13 年前

    这就是我提出的解决方案,包括门将和厄萨兹的回答。但是系统不允许我投票给你们:(

    CONCAT_WS(IFNULL(ts_usr_nameDetails.first_name,''),' ',IFNULL(ts_usr_lib_connectionNameDetails.first_name,'')) AS composerName

    这使得一些惊人的结果

        5
  •  1
  •   Community    7 年前

    这是由@chocojosh根据上述解决方案得出的答案,以及下面的另一个问题: MySQL/SQL: Update with correlated subquery from the updated table itself

    我有一个类似的问题,但是我试图连接一组concats,其中一些是空的,这导致整行为空。目的是将其他表中的数据放入主表中的单个字段中,以允许全文搜索。

    这是有效的SQL。注意,concat_ws的第一个参数是“”,这允许全文字段值之间有空格。

    希望这能帮助别人。

    update
    products target
    INNER JOIN 
    (
        select p.id, 
        CONCAT_WS(
        ' ',
            (select GROUP_CONCAT(field SEPARATOR ' ') from table1 where productId = p.id),
            p.title,' ', 
            (select GROUP_CONCAT(field, ' ', descriptions SEPARATOR ' ') from table2 where productId = p.id),
            (select GROUP_CONCAT(field SEPARATOR ' ') from table3 where productId = p.id),
            (select GROUP_CONCAT(field, ' ', catno SEPARATOR ' ') from table4 where productId = p.id),
            (select GROUP_CONCAT(field SEPARATOR ' ') from table5 where productId = p.id),
            (select GROUP_CONCAT(field SEPARATOR ' ') from table6 where productId = p.id),
            (select GROUP_CONCAT(field SEPARATOR ' ') from table7 where productId = p.id)
        ) as ft
        from products p
    ) as source
    on target.id = source.id
    set target.fulltextsearch = source.ft
    
        6
  •  1
  •   amaster    12 年前

    你也可以 COALESCE() 函数返回第一个非空值。像这样:

    SELECT CONCAT(fName1,COALESCE(CONCAT(' ',mName2,' '),' '),lName3) AS userName 
      FROM users 
      WHERE level > 10
    

    如果没有中间名,这也只会放一个空格;如果有中间名,则只放前后一个空格。

    此函数的引用可在以下位置找到: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce