MYSQL 与子查询连接

MYSQL concat with sub query

我需要连接 table 的地址字段(邮编、城市、国家名称、街道地址)。这是我写的查询,

SELECT id,concat_ws(' ',address1,zip,city,(select countryName from country where countryCode = User.countryCode))  FROM User 

它为我提供了 28 Avenue Pasteur 14390 Cabourg France 实际上应该是 27 Avenue Pasteur, 14390 Cabourg, France(逗号分隔)

我怎样才能做到这一点?

更新 使用时 SELECT id,concat_ws(',',address1,zip,city,(select countryName from country where countryCode = User.countryCode)) FROM User

它给 27 Avenue Pasteur, 14390, 卡布尔, 法国

但不是 27 Avenue Pasteur, 14390 Cabourg, 法国(14390 Cabourg 之间没有comman)

试试这个方法(还有,最好用join):

select u.id,concat(u.address1,', ',u.zip,' ',u.city,', ',c.countryName) as Address
from User u join
     country c on u.countryCode=c.countryCode

结果将是:

27 Avenue Pasteur, 14390 Cabourg, France

您应该添加逗号作为字符串的分隔符。试试这个:

SELECT id,concat_ws(',',address1,zip,city,(select countryName from country where countryCode = User.countryCode))  FROM User;