Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL IF SELECT COUNT() Is greater that zero select * from table else return nothing found

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.

like image 726
Universal Grasp Avatar asked Nov 19 '25 08:11

Universal Grasp


1 Answers

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
)
like image 64
xdazz Avatar answered Nov 20 '25 22:11

xdazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!