Selecting boolean in MySQL based on contents of another table

后端 未结 2 776
囚心锁ツ
囚心锁ツ 2021-01-28 14:47

I have the following tables:

Name: Posts

Id Creator Title Body Tags Time
1  1       Test  Test Test 123456789

and

Name: Action         


        
相关标签:
2条回答
  • 2021-01-28 14:58

    As you can see you are using deleted while in data set it is delete

    SELECT `Posts`.*,
           IF(`Action`.`Type`='delete',1,0) AS "Deleted"
    FROM `Posts`
    JOIN `Action` ON `Action`.`Target` = `Posts`.`Id`
    WHERE `Action`.`Type` != 'like'
    ORDER BY `Id` DESC;
    
    0 讨论(0)
  • 2021-01-28 15:14
    SELECT Posts.*, IF(Action.Id IS NULL,0,1) as Deleted 
    FROM Posts 
    LEFT JOIN Action 
    ON Action.Target = Posts.Id and Type='delete'
    

    If you don't care about perfomance

    SELECT Posts.*,
           exists
      (SELECT 1
       FROM Action
       WHERE Target = Posts.Id
         AND TYPE='delete') AS Deleted
    FROM Posts
    
    0 讨论(0)
提交回复
热议问题