
- 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
GROUP BY a column in another MySQL table
For this, you can use CREATE TABLE AS SELECT statement. Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> CountryName varchar(20) -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1(CountryName) values('US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1(CountryName) values('UK'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1(CountryName) values('AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1(CountryName) values('UK'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1(CountryName) values('UK'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable1(CountryName) values('US'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1(CountryName) values('AUS'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
Output
+----+-------------+ | Id | CountryName | +----+-------------+ | 1 | US | | 2 | UK | | 3 | AUS | | 4 | UK | | 5 | UK | | 6 | US | | 7 | AUS | +----+-------------+ 7 rows in set (0.00 sec)
Following is the query to GROUP BY a column in another table −
mysql> create table DemoTable2 AS select *from DemoTable group by CountryName; Query OK, 3 rows affected (0.79 sec) Records: 3 Duplicates: 0 Warnings: 0
Now check the records in the new table −
mysql> select *from DemoTable2;
Output
+----+-------------+ | Id | CountryName | +----+-------------+ | 1 | US | | 2 | UK | | 3 | AUS | +----+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- Group by one column and display corresponding records from another column with a separator in MySQL
- MySQL query to group by column and display the sum of similar values in another column
- MySQL query to select rows where column value is only 0, group by another column?
- Get first date from timestamp in MySQL group by another column with duplicate value
- Can we add a column to a table from another table in MySQL?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- Group MySQL rows in an array by column value?
- Copy column values from one table into another matching IDs in MySQL
- MySQL group by for separate id without using GROUP BY to remove duplicate column row?
- Create a table in MySQL that matches another table?
- GROUP BY and display only non-empty column values in MySQL
- Selecting a value in custom order from another column in a MySQL table with a single query
- MySQL query to group by names and display the count in a new column
- Can we GROUP BY one column and select all data in MySQL?

Advertisements