我可以将多个MySQL列连接到一个字段中吗?

时间:2019-04-25 17:38:09

标签: mysql

是否可以将billing_address_street,billing_address_city,billing_address_state,billing_address_postalcode和billing_address_country连接为一个称为地址的字段?

地址字段的预期结果如下

3401 Dufferin St,北约克,ON,M6A 2T9,加拿大

SELECT a.name                       AS Name, 
       a.billing_address_street     AS Address, 
       a.billing_address_city       AS City, 
       a.billing_address_state      AS State, 
       a.billing_address_postalcode AS Zip, 
       a.billing_address_country    AS Country, 
       a.phone_office               AS PhoneNumber1, 
       a.date_modified              AS DateModify, 
       ac.store_c                   AS Store, 
       e.email_address              AS Email 
FROM   accounts AS a, 
       accounts_cstm AS ac, 
       email_addr_bean_rel AS eb, 
       email_addresses AS e 
WHERE  a.id = ac.id_c 
       AND a.id = eb.bean_id 
       AND eb.bean_id = e.id 
       AND a.phone_office = '6471111234' 
       AND a.deleted != 1 
GROUP  BY a.id 
ORDER  BY a.date_modified 
LIMIT  1 

2 个答案:

答案 0 :(得分:2)

如果可以,请在应用程序层中执行此操作。它可以更好地处理缺少的地址组件等。

如果您确实想在数据库层中执行此操作,那么CONCAT()可以执行以下操作:

SELECT a.name                       AS Name,
       CONCAT(
         a.billing_address_street, ',',
         a.billing_address_city, ',',
         a.billing_address_state, ',', 
         a.billing_address_postalcode, ',', 
         a.billing_address_country
       ) AS address, 
       a.phone_office               AS PhoneNumber1, 
       a.date_modified              AS DateModify, 
       ac.store_c                   AS Store, 
       e.email_address              AS Email 
FROM   accounts AS a, 
       accounts_cstm AS ac, 
       email_addr_bean_rel AS eb, 
       email_addresses AS e 
WHERE  a.id = ac.id_c 
       AND a.id = eb.bean_id 
       AND eb.bean_id = e.id 
       AND a.phone_office = '6471111234' 
       AND a.deleted != 1 
GROUP  BY a.id 
ORDER  BY a.date_modified 
LIMIT  1 

答案 1 :(得分:0)