
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to update string field by concatenating to it?
For concatenating a string field, use CONCAT() function. Let us first create a table −
mysql> create table DemoTable -> ( -> SequenceId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentId) values('STU'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentId) values('STU1'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+------------+-----------+ | SequenceId | StudentId | +------------+-----------+ | 1 | STU | | 2 | STU1 | +------------+-----------+ 2 rows in set (0.00 sec)
Here is the query to update string field by concatenating to it −
mysql> update DemoTable -> set StudentId=concat(StudentId,'-','101'); Query OK, 2 rows affected (0.14 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check all the records once again from the table −
mysql> select *from DemoTable;
Output
+------------+-----------+ | SequenceId | StudentId | +------------+-----------+ | 1 | STU-101 | | 2 | STU1-101 | +------------+-----------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to group rows by the numeric characters in a string field?
- MySQL: update field with Group By?
- MySQL query to update only a single field in place of NULL
- MySQL UPDATE query where id is highest AND field is equal to variable?
- MongoDB query to update array with another field?
- MySQL query to update every alternative row string having same values?
- Is it possible to use UPDATE query with LIMIT in MySQL?
- MySQL update query to remove spaces?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- MySQL query to sort by certain last string character?
- How to run MongoDB query to update only a specific field value?
- MySQL update query to remove spaces between letters?
- MySQL query to update only month in date?
- How to update date of datetime field with MySQL?
- MySQL query to update a specific cell to be empty

Advertisements