简体   繁体   中英

Mysql how to copy data from one field to other field in same table

I have mysql table as follow:

    id      host             url
--------------------------------------
    1                   test.com/index.php
    2                   domain.com/list
    3                   www.stackoverflow.com/questions/ask

I need output as follow

    id      host                 url
------------------------------------------
    1     test.com             test.com/index.php
    2     domain.com           domain.com/list
    3     stackoverflow.com    www.stackoverflow.com/questions/ask

Thanks in advance.

Use SUBSTRING_INDEX(str, delim, count) :

UPDATE tablename
SET host = SUBSTRING_INDEX(url, '/', 1);

SQL Fiddle Demo

This will make your table looks like:

| ID |                  HOST |                                 URL |
--------------------------------------------------------------------
|  1 |              test.com |                  test.com/index.php |
|  2 |            domain.com |                     domain.com/list |
|  3 | www.stackoverflow.com | www.stackoverflow.com/questions/ask |

Assuming your table is named t :

UPDATE t
SET host=SUBSTRING_INDEX(url, '/', 1);

使用SUBSTRING_INDEX

UPDATE tbl1 SET host = SUBSTRING_INDEX(url, '/', 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM