sql >> Base de Datos >  >> RDS >> Mysql

Unir diferentes tablas según el valor de la columna

No estoy 100 % seguro de que la sintaxis sea correcta y no tengo oportunidad de probarla ahora, pero la idea debería ser clara.

SELECT DISTINCT n.id 
FROM notifications n 
JOIN (
     (SELECT b.id, 'book' AS type FROM books b WHERE b.is_visible = 1)
  UNION
     (SELECT i.id, 'interview' AS type FROM interviews i WHERE i.is_visible = 1)
) ids ON n.parent_id = ids.id AND n.parent_type = ids.type
WHERE n.user_id = 1