如何通过在 MySQL 中每条文本前面添加一行为来更新所有行?
为此,使用 CONCAT() 函数。让我们先创建一个表 −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Subject varchar(200) ); Query OK, 0 rows affected (1.36 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.12 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+----+---------+ | Id | Subject | +----+---------+ | 1 | MySQL | | 2 | MongoDB | | 3 | Java | +----+---------+ 3 rows in set (0.00 sec)
以下是如何通过添加行来更新所有行的查询 −
mysql> update DemoTable set Subject=concat("My Favourite Subject is ",Subject); Query OK, 3 rows affected (0.12 sec) Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表中的所有记录 −
mysql> select *from DemoTable;
输出
+----+---------------------------------+ | Id | Subject | +----+---------------------------------+ | 1 | My Favourite Subject is MySQL | | 2 | My Favourite Subject is MongoDB | | 3 | My Favourite Subject is Java | +----+---------------------------------+ 3 rows in set (0.00 sec)
广告