
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to update every alternative row string having same values?
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Subject varchar(100) -> ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Subject) values('C'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Subject) values('Python'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+---------+ | Id | Subject | +----+---------+ | 1 | C | | 2 | MongoDB | | 3 | Java | | 4 | MongoDB | | 5 | Python | | 6 | MongoDB | +----+---------+ 6 rows in set (0.00 sec)
Here is the query to update every second row string −
mysql> update DemoTable set Subject=replace(Subject,'MongoDB','MySQL') where Id%2=0; Query OK, 3 rows affected (0.17 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check all records from the table −
mysql> select *from DemoTable;
Output
+----+---------+ | Id | Subject | +----+---------+ | 1 | C | | 2 | MySQL | | 3 | Java | | 4 | MySQL | | 5 | Python | | 6 | MySQL | +----+---------+ 6 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL query to get sum of each column where every column has same number of values?
- MySQL query to exclude values having specific last 3 digits
- MySQL query to check if a string contains a value (substring) within the same row?
- MySQL query to update string field by concatenating to it?
- MySQL query to find alternative records from a table
- MySQL query to delete row
- MySQL update query to remove spaces?
- MySQL query to calculate the average of values in a row?
- How can I select every alternative row and display in descending order in SQL?
- Update an entire row in MySQL?
- MySQL query to select the values having multiple occurrence and display their count
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?
- How to fetch a specific row when values are the same in MySQL?
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?
Advertisements