September 5, 2017

MySQL : Conditional If-else when selection returns empty result set

I am still very new to MySQL or SQL in general. So, this is kind of new.

I need to run a select statement and if that select statement return nothing, I would like to run another different select statement :

IF EXISTS (select something from TABLE_A) THEN                                         select something from TABLE_A;                                                 
ELSE
    select something from TABLE_B;
END IF;
More from StackOverflow :

IF NOT EXISTS (SELECT ...)
BEGIN
  INSERT ...
END
And with more control :

IF EXISTS (SELECT ...)
BEGIN
  PRINT 'Do nothing.';
END
ELSE
BEGIN
  INSERT ...
END