MySQL conditions for the select Query


I am new to MySQl and not sure if this is possible or not. Let me put it across for some help. I would like to have a query which
can check a condition and decide on one of the two ‘select’ queries to be executed. Some thing like this,

if(select count(*) as test_count from test_table > '0' , select query on True , select query on false);
  1. Can something like this be done in a single query?
  2. If yes, how can I do it exactly?

Thanks in Advance

without knowing what your two queries look like, i will guess that it would probably be better to do the logic with your application language (e.g. php)

p.s. did you know we have a special forum just for mysql?


Yes, that is definitely one option. But I thought if all that could happen in a single query then it would be better.

Sorry if it was not clear. Let me explain ,

I have two tables,

Table 1: with fields is_valued and name

The table has this data :

1 - name1
1 - name2
0 - name3
1 - name4
0 - name5

Table 2: with fields is_valued and name

The table has this data :

1 - name1
1 - name2
1 - name3
1 - name4
1 - name5

So, i want a single select query that can give me all data of table1 or of table2 based on whether ‘count(*) from table1 where is_valued > 0’
is true or false respectively.

use two queries

Can you please explain a bit clearly?

first query:

SELECT COUNT(*) FROM table1 WHERE is_valued > 0

then, depending on the returned count, you would run either the query for table1 or the query for table2

I had implemented the same before I wanted to know if we can do something like that in a single query. I know believe that is the only way we can do what i want. Thanks again :):slight_smile: