
- 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
Can we GROUP BY one column and select all data in MySQL?
Yes, you can use group_concat() for this. Let us first create a table −
mysql> create table groupByOneSelectAll -> ( -> StudentDetails varchar(100), -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.91 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into groupByOneSelectAll values('StudentFirstName','John'); Query OK, 1 row affected (0.14 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName','Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName','Robert'); Query OK, 1 row affected (0.65 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName','Bob'); Query OK, 1 row affected (0.13 sec) mysql> insert into groupByOneSelectAll values('StudentFirstName','David'); Query OK, 1 row affected (0.15 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from groupByOneSelectAll;
This will produce the following output −
+------------------+-------------+ | StudentDetails | StudentName | +------------------+-------------+ | StudentFirstName | John | | StudentFirstName | Chris | | StudentFirstName | Robert | | StudentFirstName | Bob | | StudentFirstName | David | +------------------+-------------+ 5 rows in set (0.00 sec)
Following is the query to group by a single column −
mysql> select StudentDetails,group_concat(StudentName) from groupByOneSelectAll group by StudentDetails;
This will produce the following output −
+------------------+-----------------------------+ | StudentDetails | group_concat(StudentName) | +------------------+-----------------------------+ | StudentFirstName | John,Chris,Robert,Bob,David | +------------------+-----------------------------+ 1 row in set (0.05 sec)
- Related Articles
- How can we use group functions with non-group fields in MySQL SELECT query?
- Group by one column and display corresponding records from another column with a separator in MySQL
- How can we write MySQL stored procedure to select all the data from a table?
- SELECT DISTINCT vs GROUP BY in MySQL?
- MySQL query to select rows where column value is only 0, group by another column?
- Why should we not use group functions with non-group fields without GROUP BY clause in MySQL SELECT query?
- Select MySQL rows where column contains same data in more than one record?
- Listing all rows by group with MySQL GROUP BY?
- Can we select row by DATEPART() in MySQL? Is it possible?
- How to divide all columns by one column and keeping original data in R?
- How we can group data in HTML forms?
- How can we create a MySQL view with GROUP BY clause?
- How can we select a MySQL database by using PHP script?
- GROUP BY a column in another MySQL table
- MySQL Select displaying Data type in a separate column?

Advertisements