mysql if else with a statement as its condition? I am trying to calculate something from the DB level and would like to know if its possible to an if/else or ifnull or other to evaluate this?  IF not what is a good approach?
Is it possible to do something this way?
I've tried several ways, but here is my latest example.
IF (SELECT markups.markup FROM markups LIMIT 1) 
THEN
    SELECT ((SELECT AVG(products.price) FROM products WHERE products.code = &quot;8G435G232AB&quot;) * (SELECT markups.markup FROM markups LIMIT 1)) + (SELECT AVG(products.price) FROM products WHERE products.code = &quot;8G435G232AB&quot;) AS code_8G435G232AB FROM products ORDER BY products.code LIMIT 1
ELSE
    SELECT AVG(products.price) FROM products WHERE products.code = &quot;8G435G232AB&quot;
You can use dynamic sql for this purpose
The if statement chekcm if there are markups if so it tales the firwst query into a user defined variable @sql
and the executes it (see manual)
SELECT IF(EXISTS(SELECT markups.markup FROM markups)
, @sql:=' SELECT ((SELECT AVG(products.price) FROM products WHERE products.code = &quot;8G435G232AB&quot;) * (SELECT markups.markup FROM markups LIMIT 1)) + (SELECT AVG(products.price) FROM products WHERE products.code = &quot;8G435G232AB&quot;) AS code_8G435G232AB FROM products ORDER BY products.code LIMIT 1'
,@sql:='SELECT AVG(products.price) FROM products WHERE products.code = &quot;8G435G232AB&quot;');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;