

- 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
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 Questions & Answers
- 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?
- Getting Minimum and Maximum Value in MySQL
- Add to existing value in MySQL column using CONCAT function?
- Getting last 5 character of a string with MySQL query?
- MySQL CONCAT a specific column value with the corresponding record
- Getting the maximum value from a varchar field in MySQL
- Concat a field in MySQL SELECT?
- Group MySQL rows in an array by column value?
- How to determine if a value appears in a GROUP BY group in MySQL?
- Concat a string to SELECT * in MySQL?
- Use MySQL concat() and lower() effectively
- Getting a list of values by using MongoDB $group?
Advertisements