EDIT: What I said below about requiring a stored procedure is NOT TRUE. Try this:
SELECT CASE WHEN ( (SELECT COUNT(*) FROM table WHERE term LIKE "term") > 4000 )
THEN <QUERY A>
ELSE <QUERY B>
END
This is, indeed, a case expression, and it works fine outside a stored proc :-)
For instance:
mysql> SELECT CASE WHEN ( 5 > 4 ) THEN ( SELECT 'foo' ) ELSE ( SELECT 'bar' ) END;
+---------------------------------------------------------------------+
| CASE WHEN ( 5 > 4 ) THEN ( SELECT 'foo' ) ELSE ( SELECT 'bar' ) END |
+---------------------------------------------------------------------+
| foo |
+---------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> SELECT CASE WHEN ( 3 > 4 ) THEN ( SELECT 'foo' ) ELSE ( SELECT 'bar' ) END;
+---------------------------------------------------------------------+
| CASE WHEN ( 3 > 4 ) THEN ( SELECT 'foo' ) ELSE ( SELECT 'bar' ) END |
+---------------------------------------------------------------------+
| bar |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)
Old answer below for historical interest, since it already gather upvotes:
You can use the below I think, but only inside a stored procedure:
CASE (SELECT COUNT(*) FROM table WHERE term LIKE "term") > 4000
WHEN 1 THEN <QUERY A>
ELSE <QUERY B>
END CASE
This is a CASE
statement, as distinct from a CASE
expression... https://dev.mysql.com/doc/refman/5.0/en/case.html has more gory details.
Actually, I suspect in general if you want to execute different queries conditionally, you're going to need to look toward stored procedures -- I could be wrong, but that's my gut feeling at this point. If you can do it, it'll probably be with CASE expressions!
One last edit: in any real world example, I'd probably do the conditional bit in my application, and just hand off to SQL (or to an ORM which would generate my SQL) once I'd decided what to search for.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…