
- 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 select rows where column value is only 0, group by another column?
For this, use group by. Let us first create a table −
mysql> create table DemoTable1344 -> ( -> `SequenceId` int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientId int, -> isMarried tinyint(1) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1344(ClientId,isMarried) values(4567,0); Query OK, 1 row affected (0.45 sec) mysql> insert into DemoTable1344(ClientId,isMarried) values(9876,0); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1344(ClientId,isMarried) values(5432,1); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1344(ClientId,isMarried) values(4567,1); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1344(ClientId,isMarried) values(9873,0); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1344;
This will produce the following output −
+------------+----------+-----------+ | SequenceId | ClientId | isMarried | +------------+----------+-----------+ | 1 | 4567 | 0 | | 2 | 9876 | 0 | | 3 | 5432 | 1 | | 4 | 4567 | 1 | | 5 | 9873 | 0 | +------------+----------+-----------+ 5 rows in set (0.00 sec)
Following is the query to select rows where column value is only 0, group by another column −
mysql> select SequenceId,ClientId,isMarried -> from DemoTable1344 -> group by ClientId -> having isMarried=0;
This will produce the following output −
+------------+----------+-----------+ | SequenceId | ClientId | isMarried | +------------+----------+-----------+ | 1 | 4567 | 0 | | 2 | 9876 | 0 | | 5 | 9873 | 0 | +------------+----------+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- Group MySQL rows in an array by column value?
- MySQL query to select column where value = one or value = two, value = three, etc?
- MySQL query to group by column and display the sum of similar values in another column
- Exclude rows based on column value when another duplicate column value is found in MySQL?
- GROUP BY a column in another MySQL table
- How to select distinct value from one MySQL column only?
- MySQL query to select the nth highest value in a column by skipping values
- MySQL: selecting rows where a column is null?
- Select from another column if selected value is '0' in MySQL?
- Get first date from timestamp in MySQL group by another column with duplicate value
- Select MySQL rows where column contains same data in more than one record?
- MySQL query to remove a value with only numbers in a column
- GROUP BY and display only non-empty column values in MySQL
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- Find rows where column value ends with a specific substring in MySQL?

Advertisements