MySql adding a join gives incorrect results

后端 未结 2 1494
长情又很酷
长情又很酷 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: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;
    

提交回复
热议问题