MySql adding a join gives incorrect results

后端 未结 2 1471
长情又很酷
长情又很酷 2021-01-27 11:49

I\'m trying to join two tables here, so I get the total rows for a particular user based on postStatus. The data is correct without the join, but when I add a join, I get weird

相关标签:
2条回答
  • 2021-01-27 12:18

    Another possible solution:

    select
      a.userId,
      sum(a.postStatus = 0) published,
      sum(a.postStatus = 1) inactive,
      sum(a.postStatus = 5) recalled,
      sum(a.postStatus = 6) deleted,
      b.unChecked
    from userData a left outer join 
        (select userId, sum(poststatus = 10) unChecked
        from userDataMod
        group by userId)
      b on a.userId = b.userId 
     where a.userId = 1;
    
    0 讨论(0)
  • 2021-01-27 12:21

    I think you have to yoin with userid and postId

    select
      a.userId,
      sum(a.postStatus = 0) published,
      sum(a.postStatus = 1) inactive,
      sum(a.postStatus = 5) recalled,
      sum(a.postStatus = 6) deleted,
      sum(b.postStatus = 10) unChecked
    from userData a join userdatamod b on a.userId = b.userId and a.postId=b.postId where a.userId = 1;
    
    0 讨论(0)
提交回复
热议问题