通过在 MySQL 中为每行文本添加前缀来更新所有行?
mysqlmysqli database
为此,请使用 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)