Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Der bedingte mysql-Join hängt von einer Spalte ab

Sie können keine tabellenübergreifenden Joins erstellen. Am besten verwenden Sie LEFT JOIN und benutze dein CASE in Ihrem SELECT Erklärung. Sie müssen gemeinsame Spalten angeben, die Sie anzeigen möchten,

SELECT *,
        (CASE notification_base.type
            WHEN 'photo'
            THEN photo.columnName1
            ELSE post.ColumnName1
        END) as ColumnName1,
        (CASE notification_base.type
            WHEN 'photo'
            THEN photo.columnName2
            ELSE post.ColumnName2
        END) as ColumnName2
FROM notification_action
    INNER JOIN notification_base
        ON notification_action.not_base_id = notification_base.id
    INNER JOIN notification_sub
        ON notification_action.not_base_id = notification_sub.not_base_id
    INNER JOIN photo
        ON photo.id = notification_base.object_id
    INNER JOIN post
        ON post.id = notification_base.object_id
WHERE notification_sub.user_id = 3
    AND notification_sub.lastShowDate < notification_action.creationDate;