
- 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
How to determine if a value appears in a GROUP BY group in MySQL?
You can use aggregate function SUM() along with IF to determine if a value appears in a GROUP BY group.
Let us first create a demo table
mysql> create table GroupbygroupDemo -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (1.48 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into GroupbygroupDemo values(10,'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into GroupbygroupDemo values(10,'Carol'); Query OK, 1 row affected (0.08 sec) mysql> insert into GroupbygroupDemo values(10,'Carol'); Query OK, 1 row affected (0.12 sec) mysql> insert into GroupbygroupDemo values(20,'David'); Query OK, 1 row affected (0.17 sec) mysql> insert into GroupbygroupDemo values(30,'John'); Query OK, 1 row affected (0.12 sec) mysql> insert into GroupbygroupDemo values(30,'David'); Query OK, 1 row affected (0.20 sec) mysql> insert into GroupbygroupDemo values(30,'Mike'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from GroupbygroupDemo;
The output is as follows
+--------+----------+ | UserId | UserName | +--------+----------+ | 10 | John | | 10 | Carol | | 10 | Carol | | 20 | David | | 30 | John | | 30 | David | | 30 | Mike | +--------+----------+ 7 rows in set (0.00 sec)
Here is the query to determine if a value appears in a GROUP BY group
mysql> select UserId, -> if(sum(UserName='David'),'YES','NO') as Correct_Name_David -> from GroupbygroupDemo -> group by UserId;
The following is the output
+--------+--------------------+ | UserId | Correct_Name_David | +--------+--------------------+ | 10 | NO | | 20 | YES | | 30 | YES | +--------+--------------------+ 3 rows in set (0.08 sec)
- Related Articles
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- Group MySQL rows in an array by column value?
- How to implement GROUP by range in MySQL?
- GROUP BY a column in another MySQL table
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- Listing all rows by group with MySQL GROUP BY?
- How to perform conditional GROUP BY in MySQL to fetch?
- HAVING with GROUP BY in MySQL
- Getting last value in MySQL group concat?
- How to group by date regardless of time in MySQL?
- How to group objects based on a value in JavaScript?
- SELECT DISTINCT vs GROUP BY in MySQL?
- Get rows with GROUP BY in MySQL?
- How to order or choose rows in MySQL GROUP BY clause?

Advertisements