
- 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 correctly enclose subquery in MySQL?
You need to close the subquery in a parenthesis. The syntax is as follows −
select if((select count(*) from yourTableName ),'Yes','No') as anyAliasName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table SelectIfDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(10) -> ); Query OK, 0 rows affected (1.03 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into SelectIfDemo(Name) values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into SelectIfDemo(Name) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into SelectIfDemo(Name) values('Larry'); Query OK, 1 row affected (0.12 sec) mysql> insert into SelectIfDemo(Name) values('Bob'); Query OK, 1 row affected (0.22 sec) mysql> insert into SelectIfDemo(Name) values('Sam'); Query OK, 1 row affected (0.15 sec)
Now you can display all records from the table using a select statement. The query is as follows −
mysql> select *from SelectIfDemo;
The following is the output −
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Larry | | 4 | Bob | | 5 | Sam | +----+-------+ 5 rows in set (0.00 sec)
Here is the correct way to use a subquery inside enclose parenthesis. The query is as follows −
mysql> select if((select count(*) from SelectIfDemo),'Yes','No') as Result;
The following is the output −
+--------+ | Result | +--------+ | Yes | +--------+ 1 row in set (0.00 sec)
- Related Articles
- How to use % wildcard correctly in MySQL?
- How to correctly use WITH ROLLUP in MySQL?
- How to apply CROSS JOIN correctly in MySQL?
- Subquery to exclude a particular row in MySQL
- How to correctly implement conditions in MySQL stored procedure?
- How to correctly use DELIMITER in a MySQL stored procedure?
- How do I return multiple results in a MySQL subquery with IN()?
- How can we use a MySQL subquery with INSERT statement?
- How can we use a MySQL subquery with FROM clause?
- How can we create a MySQL view with a subquery?
- How can we nest a subquery within another subquery?
- How to correctly implement END IF statement in a MySQL Stored Procedure?
- How to correctly use INSERT INTO … SELECT in MySQL to avoid Error 1064?
- How can I use MySQL subquery as a table in FROM clause?
- How to correctly convert a date format into a MySQL date?

Advertisements