SET @count := (SELECT count(*)
               FROM dbo.ManyToManyTable SRSG
                        JOIN tableA SG on SRSG.a = SG.a
                        JOIN tableB SR on SRSG.b = SR.a
               WHERE SR.c = INPUT_VALUE);
IF (@count > 0)
THEN
    SELECT SG.fieldA, SG.fieldB
    FROM dbo.ManyToManyTable SRSG
             JOIN tableA SG on SRSG.a = SG.a
             JOIN tableB SR on SRSG.b = SR.a
    WHERE SR.c = INPUT_VALUE;
ELSE
    SELECT SG.a, SG.b FROM dbo.tableA SG WHERE SG.b = "default value";
END IF;
It's for a MySQL database. This works for me, but I don't like the duplicate select query. However, I have no idea how to fix it under the constraint which is the logic has be within one stored procedure.
 
     
     
    