0

One column contains a comma between data and I want to show these different data in two new columns to my question. Examples of data, column CarAndYear: [Volvo, 1995] I want the 'Volvo' to be displayed in the column Cars and the year '1995 'in the column Year. I would be very grateful for help with this MySQL issue.

4

2 回答 2

1

假设你总是有前导和后括号:

SELECT TRIM(SUBSTR(CarAndYear, 2, INSTR(CarAndYear, ',') - 2)) Cars, 
       TRIM(SUBSTR(CarAndYear, INSTR(CarAndYear, ',') + 1, LENGTH(CarAndYear) - INSTR(CarAndYear, ',') -1)) `Year`
FROM (SELECT '[Volvo, 1995]' CarAndYear) n

输出

+-------+-------+
| Cars  | Year  |
+-------+-------+
| Volvo |  1995 |
+-------+-------+
于 2013-03-05T08:08:09.943 回答
1
SELECT 
    SUBSTRING_INDEX(TRIM(LEADING '[' FROM `columnname`),',',1) as CARS,
    SUBSTRING_INDEX(TRIM(TRAILING ']' FROM `columnname`),',',-1) as Year 
FROM `tablename`;

mysql中的SUBSTRING_INDEX

mysql中的TRIM

于 2013-03-05T08:03:57.317 回答