
- 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
Getting last value in MySQL group concat?
To get last value in group concat, use SUBSTRING_INDEX(). Let us first create a table −
mysql> create table DemoTable1525 -> ( -> ListOfSubjects text -> ); Query OK, 0 rows affected (1.13 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1525 values('MongoDB,C'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1525 values('Java,C++,MySQL'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1525 values('Python,C++,C,Java'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1525;
This will produce the following output −
+-------------------+ | ListOfSubjects | +-------------------+ | MongoDB,C | | Java,C++,MySQL | | Python,C++,C,Java | +-------------------+ 3 rows in set (0.00 sec)
Here is the query to get last value in group_concat() −
mysql> select substring_index(ListOfSubjects,',',-1) as LastValue from DemoTable1525;
This will produce the following output −
+-----------+ | LastValue | +-----------+ | C | | MySQL | | Java | +-----------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- MySQL query to group concat distinct by Id?
- Getting the last 30 rows in MySQL
- How to create JSON format with group-concat in MySQL?
- Add to existing value in MySQL column using CONCAT function?
- Getting Minimum and Maximum Value in MySQL
- MySQL CONCAT a specific column value with the corresponding record
- Getting last 5 character of a string with MySQL query?
- Concat a field in MySQL SELECT?
- Getting the maximum value from a varchar field in MySQL
- How to determine if a value appears in a GROUP BY group in MySQL?
- Group MySQL rows in an array by column value?
- Concat a string to SELECT * in MySQL?
- How to use GROUP_CONCAT in CONCAT in MySQL?
- Use MySQL concat() and lower() effectively

Advertisements