trscookie,
One of the problems with pseudo-code is that it has no defined syntax, so it is perhaps too open to interpretation. :-)
mysql has
EXISTS which can be negated as
NOT EXISTS, also
if statements, a
case statement, which is almost like a
switch statement in some languages, or a
case construct in bash.
If I'm thinking correctly, and
if I'm interpreting your pseudo-code correctly, I would focus on a simplified form of the conditional part of it:
Code:
if TRUE
then
and c.name = 'variable'
else
and c.id in (select id from another_table);
end if;
I expressed it that way, to emphasize this, if the EXISTS clause is true, in a way, the clauses controlled by the if seem to evaluate to this:
Code:
and c.name = 'variable'
if false, then this clause is the result:
Code:
and c.id in (select id from another_table)
So, if the EXISTS clause is true, overall that would appear to be equivalent to this:
Code:
select c.id, c.name
from companies c
where 1 = 1
and exists (select count(*) from addresses where address = 'variable')
and c.name = 'variable' ;
whereas if false, overall it would seem to be equivalent to this:
Code:
select c.id, c.name
from companies c
where 1 = 1
and not exists (select count(*) from addresses where address = 'variable')
and c.id in (select id from another_table) ;
I created a test database, and tables with column types that should be compatible with your SQL. With that, the last two statements I illustrated above, actually run on mysql. But from that, I'm not really clear about exactly what you are trying to accomplish.
If the statements I illustrated help you at all, then great. If they don't help, perhaps you could provide a textual, not pseudo-code description, with the specifics of what you are trying to accomplish.