SQL query construction - separate data in a column

2020-04-21 02:24发布

I have a column that contains links. The problem is that the titles of the links are in the same column, so it looks like this:
linktitle|-|linkurl
I want link title and linkurl in separate columns. I've created a new column for the urls, so I'm looking for a way to extract them and update the linkurl column with them. Is there any clever way to construct a query that does this?

标签: mysql
4条回答
太酷不给撩
2楼-- · 2020-04-21 02:49
SELECT substring(field_name, 1, locate('|-|', field_name)-1) as title,
substring(field_name, locate('|-|', field_name)+3) as linkurl
查看更多
3楼-- · 2020-04-21 02:54
UPDATE tablename
SET linktitle = SUBSTRING_INDEX(link , '|-|', 1 )
linkurl = SUBSTRING_INDEX(link , '|-|', -1 )
查看更多
别忘想泡老子
4楼-- · 2020-04-21 02:56

The query that solved this problem looks like this:

UPDATE jos_fabrik_posesapp
SET linktitle = Left(poselink, InStr(poselink, '|-|')-1),
linkurl = Substring(poselink, InStr(poselink, '|-|') + 3)

I'm not quite sure what it means, but it worked. Thanks for all replies!

查看更多
乱世女痞
5楼-- · 2020-04-21 03:00
UPDATE TableName 
SET LinkTitle = Substring(LinkColumn, 0, InStr(LinkColumn, '|-|') - 1),
LinkUrl = Substring(LinkColumn, InStr(LinkColumn, '|-|') + 3)

With LinkColumn being the currently existing column, and LinkTitle & LinkUrl being where you want to store the separated data.

查看更多
登录 后发表回答