将trim函数添加到现有的sql查询中

时间:2014-12-05 06:31:13

标签: mysql joomla

我正在使用的数据库表在4个特定列中的某些值周围有引号。我已经使用以下查询来选择我需要的数据,但我希望能够从这些列中删除引号。

SELECT t1.id,
IF(
    LOCATE(' ', t1.name) > 0,
    SUBSTRING(t1.name, 1, LOCATE(' ', t1.name) - 1),
    t1.name
    ) AS memberfirst,
    IF(
    LOCATE(' ', t1.name) > 0,
    SUBSTRING(t1.name, LOCATE(' ', t1.name) + 1),
    NULL
    ) AS memberlast ,
    t1.name,
  MAX(CASE WHEN t2.`profile_key` = 'profile.address1' THEN t2.profile_value END) address,
  MAX(CASE WHEN t2.`profile_key` = 'profile.city' THEN t2.profile_value END) city,
  MAX(CASE WHEN t2.`profile_key` = 'profile.postal_code' THEN t2.profile_value END) postal_code,
  MAX(CASE WHEN t2.`profile_key` = 'profile.phone' THEN t2.profile_value END) phone
FROM users t1
LEFT JOIN user_profiles t2
  ON t1.id = t2.user_id
GROUP BY t1.id, t1.name

我将修改duoble引号的4列是:profile.address1, profile.city, profile.postal_code, and profile.phone列。我相信代码会是这样的:

TRIM(BOTH '"' FROM profile.address1) AS trimmed_profile.address1 FROM user_profiles ...但我不确定它是否可以集成在

之上

1 个答案:

答案 0 :(得分:1)

您需要使用trim()

replace()
mysql> select replace('"stackoverflow"','"','') as str;
+---------------+
| str           |
+---------------+
| stackoverflow |
+---------------+
1 row in set (0.00 sec)

因此,在您的情况下,您可以使用

MAX(CASE WHEN replace(t2.profile_key,'"','') = 'profile.address1' THEN t2.profile_value END) address 

等等。