Hi I want select query bases on some condition as below. How can I achieve ?
if var1>var2 then
SELECT * from table1;
ELSE
SELECT * from table2;
end if
Thanks in Advance.
create a STORED PROCEDURE on this,
DELIMITER $$
CREATE PROCEDURE procName(IN _val1 INT, IN _val2 INT)
BEGIN
IF _var1 > _var2 THEN
SELECT * from table1;
ELSE
SELECT * from table2;
END IF;
END $$
DELIMITER ;
and when you call the procedure,
CALL procName(1,2)
Something like this?
SELECT * from table1 WHERE var1>var2
UNION ALL
SELECT * from table2 WHERE var1<=var2
Edit: based on your comment, maybe you need something like this query:
SELECT Column1, Column2, Column3
FROM (
SELECT 1 as Tab, Column1, Column2, Column3
FROM Table1
UNION ALL
SELECT 0 as Tab, 'Value1', 'Value2', 'Value3'
) s
WHERE Tab = ((SELECT COUNT(*) FROM Table1)>10)
this will return all rows from table1 if there are more than 10 rows, or it will return a single row (not present in the database) otherwise.
var*are externally allocated variables? Or fetched from table?