我想将MySQL数据库的一列拆分为两个不同的列。
在这里,我想分割" long"中的每个值。 ,
后的列,并更新" lat"列与相关的一半分裂。
答案 0 :(得分:1)
您可以将substring_index
功能用作
mysql> select substring_index('N27.1963456,E85.234567',',',-1);
+--------------------------------------------------+
| substring_index('N27.1963456,E85.234567',',',-1) |
+--------------------------------------------------+
| E85.234567 |
+--------------------------------------------------+
因此更新命令将为
update your_table set `lat` = substring_index(`long`,',',-1);
<强>更新强> 如何删除第一个字符
mysql> select RIGHT('foobarbar', length('foobarbar')-1);
+-------------------------------------------+
| RIGHT('foobarbar', length('foobarbar')-1) |
+-------------------------------------------+
| oobarbar |
+-------------------------------------------+
mysql> select RIGHT('E85.234567', length('E85.234567')-1);
+---------------------------------------------+
| RIGHT('E85.234567', length('E85.234567')-1) |
+---------------------------------------------+
| 85.234567 |
+---------------------------------------------+
所以你可以这样做
update your_table set `lat` = RIGHT(`lat`,length(`lat`)-1);
答案 1 :(得分:1)
这可能会更新两列:
UPDATE mytable
SET `lat` = TRIM(SUBSTR(`long`,1,INSTR(`long`,',')-1))
, `long` = TRIM(SUBSTR(`long`,INSTR(`long`,',')+1));
示例输出:
+-------------+--------------+
| long | lat |
+-------------+--------------+
| E85.2463446 | N27.19638836 |
+-------------+--------------+
在MySQL 5.5中使用SQLFiddle示例:http://sqlfiddle.com/#!2/89a5d7/1/0
编辑:删除第一个字符。实际上将N / S处理为+/-和E / W为+/-:
UPDATE mytable
SET `lat` = REPLACE(
REPLACE(
TRIM(SUBSTR(`long`,1,INSTR(`long`,',')-1))
,'N','')
,'S','-')
, `long` = REPLACE(
REPLACE(
TRIM(SUBSTR(`long`,INSTR(`long`,',')+1))
,'E','')
,'W','-');
输入:
insert into mytable (`long`) VALUES ('N27.1963, E85.2463446');
insert into mytable (`long`) VALUES ('S37.196336, E104.2463446');
insert into mytable (`long`) VALUES ('N47.19638836, W6.2463446');
输出:
+-------------+-------------+
| long | lat |
+-------------+-------------+
| 85.2463446 | 27.1963 |
| 104.2463446 | -37.196336 |
| -6.2463446 | 47.19638836 |
+-------------+-------------+