
- 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
Update a table in MySQL and display only the initials name in a new column
To get the initial, use the concept of left() along with substring_index().
Let us create a table −
mysql> create table demo13 −> ( −> full_name varchar(100), −> short_name varchar(20) −> ); Query OK, 0 rows affected (1.18 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo13(full_name) values('John Smith'); Query OK, 1 row affected (0.27 sec) mysql> insert into demo13(full_name) values('David Miller'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo13(full_name) values('Chris Brown'); Query OK, 1 row affected (0.28 sec)
Display records from the table using select statement −
mysql> select *from demo13;
This will produce the following output −
+--------------+------------+ | full_name | short_name | +--------------+------------+ | John Smith | NULL | | David Miller | NULL | | Chris Brown | NULL | +--------------+------------+ 3 rows in set (0.00 sec)
Following is the query to update table and get the initials name −
mysql> update demo13 −> set short_name= concat( −> left(full_name, 1), −> left(substring_index(full_name, ' ', −1), 1) −> ); Query OK, 3 rows affected (0.14 sec) Rows matched: 3 Changed: 3 Warnings: 0
Display records from the table using select statement −
mysql> select *from demo13;
This will produce the following output −
+--------------+------------+ | full_name | short_name | +--------------+------------+ | John Smith | JS | | David Miller | DM | | Chris Brown | CB | +--------------+------------+ 3 rows in set (0.00 sec)
- Related Articles
- Update only a single column in a MySQL table and increment on the basis of a condition
- Update only a single column value in MySQL
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- Best way to update a single column in a MySQL table?
- How to round MySQL column with float values and display the result in a new column?
- Find and display duplicate values only once from a column in MySQL
- MySQL query to group by names and display the count in a new column
- Selecting and displaying only some rows from a column in a MySQL table
- Multiply values of two columns and display it a new column in MySQL?
- Extract the middle part of column values in MySQL surrounded with hyphens and display in a new column?
- Display distinct column name in MySQL
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
- Display MySQL Database, Table, and Column Information
- Divide numbers from two columns and display result in a new column with MySQL
- MySQL query to count occurrences of distinct values and display the result in a new column?

Advertisements