Set a specific value for the first three column values in MySQL?


To set a specific value for only 1st three values, you need to use LIMIT 3. Let us first create a table −

mysql> create table DemoTable1968
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(20)
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1968(Name) values('Chris');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1968(Name) values('David');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1968(Name) values('Sam');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1968(Name) values('Mike');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1968(Name) values('Carol');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1968;

This will produce the following output −

+----+-------+
| Id | Name  |
+----+-------+
|  1 | Chris |
|  2 | David |
|  3 | Sam   |
|  4 | Mike  |
|  5 | Carol |
+----+-------+
5 rows in set (0.00 sec)

Here is the query to set a specific value for the first three column values −

mysql> update DemoTable1968
   set Name='Robert'
   order by Id
   limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3 Warnings: 0

Let us check the table records once again:

mysql> select * from DemoTable1968;

This will produce the following output −

+----+--------+
| Id | Name   |
+----+--------+
|  1 | Robert |
|  2 | Robert |
|  3 | Robert |
|  4 | Mike   |
|  5 | Carol  |
+----+--------+
5 rows in set (0.00 sec)

Updated on: 31-Dec-2019

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements