I’ve a tableA with 2 columns (value1, value2) with a CSV formatted value. I want to create a tableB with the same fields as tableA but, in those 2 columns, with just a limit number of values (note that I don’t know how many characters exists between commas). This “limit” is the first n elements of the CSV.
Table A
+----+----------+----------------------+----------------------+
| id | other | value1 | value2 |
+----+----------+----------------------+----------------------+
| 1 | other1 | 111,222,333,444 | 1616,17,1,8,19,2020 |
| 2 | other2 | 55,6,7777,8,9 | 21,22,23 |
| 3 | other3 | 10101,11.11,13,14,15 | 242424,2525,26,27 |
+----+----------+----------------------+----------------------+
Table B (limit to 3 elements)
+----+----------+----------------------+----------------------+
| id | other | value1 | value2 |
+----+----------+----------------------+----------------------+
| 1 | other1 | 111,222,333 | 1616,17,1 |
| 2 | other2 | 55,6,7777 | 21,22,23 |
| 3 | other3 | 10101,11.11,13 | 242424,2525,26 |
+----+----------+----------------------+----------------------+
EDITED:
I didn’t mention that those tables have a huge number of elements (and many other columns like “other”). Therefore I prefer to use:
create table tableB LIKE tableA;
INSERT tableB SELECT * FROM tableA;
and then update the value1 and value2 (in 2 distinct transactions – in fact I’ve 91 columns like that). This probably is more efficient then a “create table” with all the values inside in a single query…
EDITED2 (solution):
SQLFiddle:
You cam use MySQL’s
SUBSTRING_INDEX()function, together withCREATE TABLE ... SELECT:See it on sqlfiddle.