【问题标题】:MySQL CONCAT multiple fields with IF statementMySQL CONCAT 多个字段与 IF 语句
【发布时间】:2013-01-09 17:12:06
【问题描述】:

我正在尝试做一些我认为很简单的事情,但我被困住了。我基本上想从多个地址部分字段创建一个地址字段,使用 IF 语句来使用地址或交叉点。这是我的声明:

        CONCAT(loc_name,'\n',
            IF ( add_number != '' && add_street != '' ) THEN 
                CONCAT(add_number,' ',add_street,'\n')
            ELSEIF ( x_street_1 != '' && x_street_2 != '' ) THEN 
                CONCAT(x_street_1,' & ',x_street_2,'\n')
            END IF
        ,city,', ', 
            IF ( state != '') THEN 
                CONCAT(state,' ',country,'\n')
            ELSEIF ( x_street_1 != '' && x_street_2 != '' ) THEN 
                CONCAT(country,'\n')
            END IF
        ) AS loc_info

但它根本不喜欢我正在做的事情,它会在以下位置引发错误:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') THEN \n\t\t\t\t\t\tadd_number,' ',add_street,'\n'\n\t\t\t\t\tELSEIF ( x_street_1 != '' && x_"

这似乎不喜欢我的空字段 ('') 符号。但我不知道为什么。我不能像这样在 CONCAT 中使用 IF 语句吗?

感谢您的任何见解。

【问题讨论】:

    标签: mysql if-statement concat


    【解决方案1】:

    IIRC,你要使用的语法是

    IF(condition, expression_if_true, expression_if_false)
    

    我可能是错的,但你可能想试试。

    【讨论】:

      【解决方案2】:

      语法不正确。你想用CASE:

      SET @loc_name = 'Location';
      SET @add_street = 'Add Street';
      SET @add_number = '10';
      SET @x_street_1 = 'Street 1';
      SET @x_street_2 = 'Street 2';
      SET @city = 'City';
      SET @state = 'State';
      SET @country = 'Country';
      
      SELECT Concat(@loc_name, '\n', CASE 
                                       WHEN @add_number != '' 
                                            AND @add_street != '' THEN 
                                       Concat(@add_number, ' ', @add_street, '\n') 
                                       WHEN @x_street_1 != '' 
                                            AND @x_street_2 != '' THEN 
                                       Concat(@x_street_1, ' & ', @x_street_2, 
                                       '\n') 
                                     end, @city, ', ', CASE 
                                                         WHEN @state != '' THEN 
             Concat(@state, ' ', @country, '\n') 
                    WHEN ( @x_street_1 != '' 
                           AND @x_street_2 != '' ) THEN Concat(@country, '\n') 
                                                       end) AS loc_info 
      

      结果

      | LOC_INFO | ----------------------------------------------------------- |地点 10 加街 城市,州国家 |

      只需找到 @ 并将其替换为

      【讨论】:

      【解决方案3】:

      这也可能有帮助:

      CONCAT(loc_name,'\n',
                  IF ( add_number != '' && add_street != '' , 
                      CONCAT(add_number,' ',add_street,'\n'),
                      IF ( x_street_1 != '' && x_street_2 != '' , 
                         CONCAT(x_street_1,' & ',x_street_2,'\n'),""
                         )
                     ),
      
              city,
               ',' , 
                  IF ( state != '',
                      CONCAT(state,' ',country,'\n'),
                      IF ( x_street_1 != '' && x_street_2 != '' , 
                         CONCAT(country,'\n'),""
                         )
                     ) AS loc_info
      

      还有你在这里比较state != '' 是针对空值吗?
      如果是这样这会给你错误的答案你必须使用state IS NOT NULL而不是那个。

      【讨论】:

      • 很高兴了解 null。但我相信这些都是空的、非空的字段。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 2012-03-27
      • 1970-01-01
      • 2013-09-14
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多