Update all rows by prefixing a line to every text in MySQL?


For this, use the CONCAT() function. Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Subject varchar(200)
   );
Query OK, 0 rows affected (1.36 sec)

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+----+---------+
| Id | Subject |
+----+---------+
| 1  | MySQL   |
| 2  | MongoDB |
| 3  | Java    |
+----+---------+
3 rows in set (0.00 sec)

Here is the query to update all rows by prefixing a line −

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

Let us check the all records once again from table −

mysql> select *from DemoTable;

Output

+----+---------------------------------+
| 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)

Updated on: 30-Jul-2019

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements