- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set different IDs for records with conditions using a single MySQL query
For conditions, use CASE statement in MySQL. Let us first create a table −
mysql> create table DemoTable1545 -> ( -> Id int, -> FirstName varchar(20) -> ); Query OK, 0 rows affected (1.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1545 values(1,'John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1545 values(2,'Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1545 values(3,'Bob'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1545;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 1 | John | | 2 | Chris | | 3 | Bob | +------+-----------+ 3 rows in set (0.00 sec)
Here is the query to set different IDs using CASE statement −
mysql> update DemoTable1545 -> set Id= -> case when FirstName like '%John%' then 101 -> when FirstName like '%Chris%' then 201 -> when FirstName like '%Bob%' then 501 -> else Id -> end; Query OK, 3 rows affected (0.18 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1545;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 101 | John | | 201 | Chris | | 501 | Bob | +------+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- MySQL update multiple records in a single query?
- Delete records from a MySQL table with IN() in a single query
- MySQL Query to set currency records
- Select count of values (Yes, No) with same ids but different corresponding records in MySQL?
- MySQL query to set different combinations for values in a table?
- How to set different auto-increment ids for two tables with a user-defined variable?
- Can we get records “Jone Deo” or “Deo Jone” with a single MySQL query?
- Counting different distinct items in a single MySQL query?
- MySQL query with OR & AND conditions
- Using MySQL keywords in a query surrounded with single quotes?
- Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?
- Use UNION ALL to insert records in two tables with a single query in MYSQL

Advertisements