Joining different tables based on column value

前端 未结 1 425
南方客
南方客 2020-12-13 10:00

I have a table called notifications:

CREATE TABLE `notifications` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT         


        
相关标签:
1条回答
  • 2020-12-13 10:41

    I'm not 100% sure the syntax is right and I have no chance to test it right now, but the idea should be clear.

    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
    
    0 讨论(0)
提交回复
热议问题