MySQL使用IF语句CONCAT多个字段

时间:2013-01-25 17:11:42

标签: mysql if-statement concat

我正在尝试做一些我认为很简单的事情,但我被困住了。我基本上想要从多个地址部分字段创建单个地址字段,使用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语句吗?

感谢您的任何见解。

3 个答案:

答案 0 :(得分:9)

IIRC,您要使用的语法是

IF(condition, expression_if_true, expression_if_false)

我可能错了,但你可能想尝试一下。

答案 1 :(得分:3)

语法不正确。您想使用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 |
-----------------------------------------------
| Location
10 Add Street
City, State Country
 |

只需使用@找到并替换

答案 2 :(得分:2)

这也可能会有所帮助:

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,' &amp; ',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代替。

相关问题