Supposing a MySQL 5.7 database with a schema named myschema, where exists only one table of these three: table_a, table_b, or table_c. If there is only one table, for example table_a, the following query throws a Error Code: 1146. Table 'myschema.table_b' doesn't exist.
UPDATE myschema.table_d d
SET d.d_value =
(CASE
WHEN EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'myschema'
AND TABLE_NAME = 'table_a')
THEN (SELECT MAX(a.a_value)
FROM myschema.table_a a
WHERE a.table_d_id = d.id)
WHEN EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'myschema'
AND TABLE_NAME = 'table_b')
THEN (SELECT MAX(b.b_value)
FROM myschema.table_b b
WHERE b.table_d_id = d.id)
WHEN EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'myschema'
AND TABLE_NAME = 'table_c')
THEN (SELECT MAX(c.c_value)
FROM myschema.table_c c
WHERE c.table_d_id = d.id)
ELSE 0
END);
So I suppose it's necessary that all tables must exist to perform the case when [boolean expression] then select [value] from [table] statements. Am I sure? If so, is there another way to do this? Thanks in advance.