我有一个mysql查询,工作正常,但结果带有尾随和引导引号。这是如何:
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',1), ',', -1) as first_col,
SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',2), ',', -1) as second_col,
SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',3), ',', -1) as third_col,
SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',4), ',', -1) as fourth_col
FROM (
select
SELECT GROUP_CONCAT(column_name) as concat_values
FROM my_table t1
GROUP BY (uid)
) t;
结果如下:
Column1 Column2 Column3
["Result_1"] ["Result_2"] ["Result_3"]
我想修剪括号([])和引号(""),以便结果显示为' Result_1'。已尝试使用TRIM(BOTH)
,但我收到语法错误。
我最好如何修剪它?
答案 0 :(得分:1)
这将有效:
$str = '["Result_1"]';
echo trim($str, '"');
$str = '["Result_1"]';
echo trim($str, '[');
$str = '["Result_1"]';
echo trim($str, ']');
答案 1 :(得分:1)
这可以解决你在mysql中的问题
SELECT TRIM(BOTH '"' FROM TRIM(TRAILING ']' FROM TRIM(LEADING '[' FROM '["test"]')))
由于性能方面的考虑,我更喜欢修剪应用程序中的字符串。
以下是如何为sql添加修剪
SELECT
TRIM(BOTH '"' FROM TRIM(TRAILING ']' FROM TRIM(LEADING '[' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',1), ',', -1)))) AS first_col,
TRIM(BOTH '"' FROM TRIM(TRAILING ']' FROM TRIM(LEADING '[' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',2), ',', -1)))) AS second_col,
TRIM(BOTH '"' FROM TRIM(TRAILING ']' FROM TRIM(LEADING '[' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',3), ',', -1)))) AS third_col,
TRIM(BOTH '"' FROM TRIM(TRAILING ']' FROM TRIM(LEADING '[' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(concat_values, ',',4), ',', -1)))) AS fourth_col
FROM (
select
SELECT GROUP_CONCAT(column_name) as concat_values
FROM my_table t1
GROUP BY (uid)
) t;