
- 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
Call aggregate function in sort order with MySQL
For this, use GROUP_CONCAT() along with ORDER BY clause. Let us first create a table −
mysql> create table DemoTable1588 -> ( -> StudentId int, -> StudentFirstName varchar(20), -> StudentMarks int -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1588 values(110,'Bob',78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1588 values(101,'Sam',78); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1588 values(105,'Mike',78); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1588;
This will produce the following output −
+-----------+------------------+--------------+ | StudentId | StudentFirstName | StudentMarks | +-----------+------------------+--------------+ | 110 | Bob | 78 | | 101 | Sam | 78 | | 105 | Mike | 78 | +-----------+------------------+--------------+ 3 rows in set (0.00 sec)
Here is the query to call aggregate function in sort order −
mysql> select group_concat(StudentFirstName order by StudentId separator '--') from DemoTable1588 -> group by StudentMarks;
This will produce the following output −
+------------------------------------------------------------------+ | group_concat(StudentFirstName order by StudentId separator '--') | +------------------------------------------------------------------+ | Sam--Mike--Bob | +------------------------------------------------------------------+ 1 row in set (0.04 sec)
- Related Articles
- How MySQL aggregate functions can be combined with MySQL IF() function?
- Get the maximum value of a column with MySQL Aggregate function
- Implement Custom Sort Order in MySQL
- How to sort by value with MySQL ORDER BY?
- Select aggregate function and all other columns in MySQL
- Sort values that contain letters and symbols in custom order with MySQL
- MySQL Aggregate Function to find the number of occurrences?
- MongoDB aggregate query to sort
- Sort by date & time in descending order in MySQL?
- How to add column values in MySQL without using aggregate function?
- Find the average of column values in MySQL using aggregate function
- What is the default sort order in MySQL tables?
- How can we sort MySQL output in descending order?
- How can we sort MySQL output in ascending order?
- Resolve ERROR 1111 (HY000): Invalid use of group function in MySQL? How to correctly use aggregate function with where clause?

Advertisements