Try this:
IF EXISTS (SELECT * FROM Table1 WHERE <CONDITION>) THEN
BEGIN
INSERT INTO Table2 ...
END
ELSE
BEGIN
INSERT INTO Table3...
<DO SOME more manipulations>
END
END IF
Check IF Statement in MySQL
Question
I want to do something like
IF (EXISTS (select * from Table1 where <Condition>))
{
INSERT INTO Table2 ...
}
ELSE
{
INSERT INTO Table3...
<Do some more manipulations>
}
AFAIK I can only use the IF
as part of a statement.
Is there a way to do it?
Thanks,
==Edit 1==
I would like to do this logic as part of a Stored Procedure.
Solution
Try this:
IF EXISTS (SELECT * FROM Table1 WHERE <CONDITION>) THEN
BEGIN
INSERT INTO Table2 ...
END
ELSE
BEGIN
INSERT INTO Table3...
<DO SOME more manipulations>
END
END IF
Check IF Statement in MySQL
OTHER TIPS
The only way is by calling a stored procedure.
Using stored procedure, you can define variables, check multiple conditions, execute multiple statements, etc.
But, just with IFEXISTS
and ELSE
at max you can execute 2 statements in straight.
References: MySQL: CREATE PROCEDURE Syntax