所以我有一个地址 TEXT 字段,我需要将其拆分为单独的字段,例如:“physical_address”字段,内容如下:
| physical_address |
+------------------+
| 123 Street Name |
| Suburb |
| City |
+------------------+
会成为:
| physical_address_1 | physical_address_2 | physical_address_3 |
+--------------------+--------------------+--------------------+
| 123 Street Name | Suburb | City |
+--------------------+--------------------+--------------------+
现在这只是暂时的,因为我需要以上述格式将其导入应用程序,因此选择语句将是最佳选择 - 尽管我想它不会那么简单。
这种作品,但不完全是:
SELECT
physical_address,
SUBSTRING_INDEX(physical_address, CHAR(10), -5) AS a,
SUBSTRING_INDEX(physical_address, CHAR(10), -4) AS b,
SUBSTRING_INDEX(physical_address, CHAR(10), -3) AS c,
SUBSTRING_INDEX(physical_address, CHAR(10), -2) AS d,
SUBSTRING_INDEX(physical_address, CHAR(10), -1) AS e
FROM clients LIMIT 5
使用 5 行 TEXT 值,可以完美运行,少一点,您会看到前几个字段(取决于行)将重复值。无法为此设置 SQL Fiddle,因为它似乎正在清理我的返回提要,所以这里是示例架构:
CREATE TABLE clients (id int unsigned auto_increment primary key, physical_address text);
INSERT INTO clients (id,physical_address) VALUES (1,"123 Street,\r\n123 Suburb,\r\n123 City");
INSERT INTO clients (id,physical_address) VALUES (2,"456 Street,\r\n456 Suburb,\r\n456 City,\r\n456 Province");
INSERT INTO clients (id,physical_address) VALUES (3,"789 Street,\r\n789 Suburb,\r\n789 City,\r\n789 Province,\r\n789 Country");