0
notificationTable
ID              type        typeID      userID
==============================================
1               comment     34          2
2               accept      22          2

eventTable
ID              event       content
===================================
21              post        34  
22              accept      22

commentTable
ID              comment     eventID
===================================
34              'test'      21

列タイプの値に応じて異なるテーブルを結合したい。これは私の現在の擬似コードです:

SELECT *
FROM notificationTable notification
IF (notification.type == 'comment')
    LEFT JOIN commentTable comment
    ON notification.typeID = comment.ID
ELSEIF (notification.type == 'accept')
    LEFT JOIN evenTable event
    ON notification.typeID = event.ID
WHERE notification.userID = 2

誰かが私の意図の本当の質問を知っていますか?

4

1 に答える 1

2

何かのようなもの

SELECT *
FROM notificationTable notification   
LEFT JOIN commentTable comment
ON (notification.typeID = comment.ID AND notification.type == 'comment')
LEFT JOIN evenTable event
ON (notification.typeID = event.ID AND notification.type == 'accept')
WHERE notification.userID = 2

しかし、ポリモーフィックな関係を避けるためにシステムを再設計することを強くお勧めします...

于 2013-01-04T18:01:28.770 に答える