Complex Sql Query, Checking Column Values In Multiple Tables
I have a pretty huge SQL query to check for notifications, and I have several different types of notifications in the table, IE: posts, likes, comments, photoComments, photoLikes,
Solution 1:
I think you just want to join the three tables together in a single query:
n.uniqueID =ANY (
SELECT photos.id
FROM photos INNERJOIN
posts
ON photos.id=posts.post innerjoin
likes
on posts.id = likes.postId
WHERE photos.state=0and
posts.state=0and
likes.state =0
)
Your logic is not to return when there is a like or post with the state of 0. It seems to be that all the likes and posts have a state of zero. For this, do an aggregation with a having clause:
n.uniqueID =ANY (
SELECT photos.id
FROM photos INNERJOIN
posts
ON photos.id=posts.post innerjoin
likes
on posts.id = likes.postId
where photos.state =0groupby photos.id
havingMAX(posts.state) =0andMAX(likes.state) =0
Post a Comment for "Complex Sql Query, Checking Column Values In Multiple Tables"