Question

Am looking for a way to have a query like:

Mysql:

Example: IF SELECT COUNT(column_XX) WHERE column_XX = value_XX FROM table_XX is greater that zero (0) then SELECT everything or SELECT * FROM that table_XX WHERE column_XX = value_XX and return all data ... else return 'nothing found' or just ZERO (0)

In simple words,I want to select all the data from a table column if all the values in the column is greater than zero else select only the values>=1 and return all the values.

Which Magic can be applied here to have it done in a SINGLE QUERY?... am really Stuck thanks.

Was it helpful?

Solution

You could try like this:

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
)

OTHER TIPS

Would this work !

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top