技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

在 MySQL 中更新所有 varchar 列行以在斜线前显示值?

mysqlmysqli database

为此,请使用 UPDATE 命令和 SUBSTRING_INDEX()。让我们首先创建一个表 −

mysql> create table demo69
−> (
−> name varchar(40)
−> );
Query OK, 0 rows affected (5.04 sec)

使用 insert 命令向表中插入一些记录 −

mysql> insert into demo69 values('John/Smith');
Query OK, 1 row affected (0.83 sec)

mysql> insert into demo69 values('David/Miller');
Query OK, 1 row affected (0.23 sec)

mysql> insert into demo69 values('Chris/Brown');
Query OK, 1 row affected (0.40 sec)

mysql> insert into demo69 values('Carol/Taylor');
Query OK, 1 row affected (0.36 sec)

使用 select 语句显示表中的记录 −

mysql> select *from demo69;

这将产生以下输出 −

+--------------+
| name         |
+--------------+
| John/Smith   |
| David/Miller |
| Chris/Brown  |
| Carol/Taylor |
+--------------+
4 rows in set (0.03 sec)

以下是更新所有 varchar 列行的查询 −

mysql> update demo69
−> set name=substring_index(name,'/',1);
Query OK, 4 rows affected (0.13 sec)
Rows matched: 4 Changed: 4 Warnings: 0

使用 select 语句显示表中的记录 −

mysql> select *from demo69;

这将产生以下输出 −

+-------+
| name  |
+-------+
| John  |
| David |
| Chris |
| Carol |
+-------+
4 rows in set (0.00 sec)

相关文章