字段值之前的mySQL空间

时间:2012-08-16 15:04:18

标签: mysql

如果值不为空,我试图放入一个空格,但是当我运行查询时它似乎只输出一个数字?

CONCAT(address, IF(address2 = '', '', ' ' & address2), ' ', city, ', ', state, ' ', zip) AS theAddress

以上的查询输出是:

1234 N. Shore Ave.0 Burbank, CA 41577

输出应如下所示:

1234 N. Shore Ave. Apartment 223 Burbank, CA 41577

我在这里做错了什么?

2 个答案:

答案 0 :(得分:4)

试试这个:

CONCAT(address, IF(address2 = '', '', CONCAT(' ', address2)), ' ', city, ', ', state, ' ', zip) AS theAddress

我认为你的代码正在做的是空格和地址2之间的逻辑AND

答案 1 :(得分:2)

我认为它应该是另一个concat而不是&

CONCAT(address, IF(address2 = '', '', CONCAT(' ',  address2)), ' ', city, ', ', state, ' ', zip) AS theAddress
                                         ^ here

' ' & address2返回0和1(真或假)