
- 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
MySQL query to find the number of occurrences from two columns?
Use MySQL GROUP_BY to find the number of occurrences from two columns. Let us first create a table −
mysql> create table DemoTable -> ( -> Name1 varchar(20), -> Name2 varchar(20) -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John','Adam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris','David'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Robert','Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('David','Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Mike','Robert'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+--------+ | Name1 | Name2 | +--------+--------+ | John | Adam | | Chris | David | | Robert | Mike | | David | Chris | | Mike | Robert | +--------+--------+ 5 rows in set (0.00 sec)
Here is the query to find the number of occurrences from two columns −
mysql> select greatest(Name1,Name2),least(Name1,Name2),count(*) as Occurrences from DemoTable -> group by greatest(Name1,Name2),least(Name1,Name2);
This will produce the following output −
+-----------------------+--------------------+-------------+ | greatest(Name1,Name2) | least(Name1,Name2) | Occurrences | +-----------------------+--------------------+-------------+ | John | Adam | 1 | | David | Chris | 2 | | Robert | Mike | 2 | +-----------------------+--------------------+-------------+ 3 rows in set (0.02 sec)
- Related Articles
- MySQL Aggregate Function to find the number of occurrences?
- MySQL query to count the number of 0s and 1s from a table column and display them in two columns?
- MySQL query to calculate the days between two dates from different columns but similar rows
- MySQL query to sum the values of similar columns from two different tables for a particular ID
- MySQL query to find the number of rows in the last query
- Update two columns with a single MySQL query
- MySQL query to combine two columns in a single column?
- MySQL query to delete a row if two columns are equal
- How to count the number of occurrences of a specific value in a column with a single MySQL query?
- Count the occurrences of specific records (duplicate) in one MySQL query
- How to find the number of columns in a MySQL table?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- MySQL query to display ranks of multiple columns?
- MySQL query to find the top two highest scores
- Comparing two columns in a single MySQL query to get one row?

Advertisements