0

我有mysql表如下:

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

我需要如下输出

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

提前致谢。

4

3 回答 3

4

使用SUBSTRING_INDEX(str, delim, count)

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

SQL 小提琴演示

这将使您的表格看起来像:

| 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 |
于 2013-03-06T06:53:31.643 回答
2

假设您的表名为t

UPDATE t
SET host=SUBSTRING_INDEX(url, '/', 1);
于 2013-03-06T06:53:16.853 回答
0

使用SUBSTRING_INDEX

UPDATE tbl1 SET host = SUBSTRING_INDEX(url, '/', 1)
于 2013-03-06T06:54:33.093 回答