简体   繁体   English

MySQL如何将数据从一个字段复制到同一表的另一个字段

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

I have mysql table as follow: 我有mysql表,如下所示:

    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) : 使用SUBSTRING_INDEX(str, delim, count)

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

SQL Fiddle Demo SQL小提琴演示

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 : 假设您的表名为t

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

使用SUBSTRING_INDEX

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从同一mysql数据库表中的另一个字段移动一个字段数据? - How to move one fields data from another field within same mysql database table? MYSQL将字段从一个表复制到另一表 - MYSQL Copy field from one table to another table 将mysql blob字段从一个数据库复制到另一个数据库 - copy mysql blob field from one database to the other mysql如何根据一个字段和另一个表中的一个字段显示表的某些行? - mysql how to show some rows of table depending on one field and depending on one field in other table? MySQL-从另一个表中查询与当前表相同的ID的字段 - mySQL - Querying a field from another table with the same id as in the current one 如何从MySQL表中提取两个具有相同字段值而另一字段不同的行? - How do I pull two rows from a MySQL table that have one field value in common and the other field different? 如何从一个表数据复制列到另一表并在mysql中同时插入更多列? - how to copy column from one table data to another table and insert more columns same time in mysql? 使用另一个表中的字段引用一个 MySQL 表中的数据 - Reference data from one MySQL table by using a field from another MYSQL连接具有相同字段值的两个表字段,但如果另一个字段与另一个字段不匹配,则仍将包括 - MYSQL Join two table fields with same field value but will still include if the other field doesn't match the other one 将来自mysql的表数据加载到一个输入字段中 - load table data from mysql into one input field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM