MYSQL IF SELECT COUNT () More than null selection * from table else return nothing found

I am looking for a way to get a request like:

Mysql:

Example: IF SELECT COUNT(column_XX) WHERE column_XX = value_XX FROM table_XXmore than zero ( 0), and then SELECTeverything or SELECT * FROM, which table_XX WHERE column_XX = value_XXreturns all the data ... else return "nothing was found" or just ZERO ( 0)

In simple words, I want to select all the data from the column of the table if all the values ​​in the column are greater than zero, otherwise we will select only the values> = 1 and return all the values.

What magic can I use here to do this in SINGLE QUERY ? ... I'm really thankful.

+3
source share
2 answers

You can try the following:

SELECT * FROM table_XX
WHERE column_XX = (
  SELECT column_XX FROM table_XX 
  WHERE column_XX = value_XX GROUP BY column_XX HAVING COUNT(column_XX) > 0
)
+3

!

SELECT * FROM test_enum;
+------+-------+----------+
| ID   | Name  | IsActive |
+------+-------+----------+
|    1 | Abdul | No       |
|    1 | Abdul | Yes      |
+------+-------+----------+
2 rows in set (0.00 sec)

SELECT * FROM test_enum WHERE EXISTS(SELECT * FROM test_enum WHERE IsActive = 'No') AND IsActive = 'No';
+------+-------+----------+
| ID   | Name  | IsActive |
+------+-------+----------+
|    1 | Abdul | No       |
+------+-------+----------+
1 row in set (0.00 sec)
0

All Articles