mysql - unexpected results of join and count in jpa query -
i want retrieve count of tags used among posts specific user. e.g. if 1 user has 3 posts written , 2 posts tagged 'sometag', query should written count 2 tag 'sometag'
there 1 many relationship between user , post. there many many relationship between post , tag using eclipselink jpa wrote jpa query follows
select count(tag.tagname) post post, tag tag join tag.posts posts post.user = :user , tag.tagname = :tagname
but giving me multiplication of tag , posts have tagged 9 posts tag 'friends , have total 17 posts , result getting 153 i.e. multiplication.
i tried simple mysql query follows
select count(*) tag t join post_tag pt on pt.idtag = t.idtag join post p on pt.idpost = p.idpost join user u on p.iduser = u.iduser u.username = 'prasadkharkar' , t.tagname = 'friends'
this works fine , gives me result 9.
i want result in jpa query. can please explain me wrong thing doing?
by selecting post , tag, you're doing cartesian product. in sql query, , use joins:
select count(tag.tagname) tag tag join tag.posts post post.user = :user , tag.tagname = :tagname
Comments
Post a Comment