I have three users: user 1 has two posts, user 2 has one post, user 3 has one post. No comments have been added.
Yet if I run the following query I only get one post for user with ID 1, and one post for user with ID 2. I get nothing for user with ID 3.
However, if I remove the COUNT from the comments table, all posts show up. Any ideas as to what I’m doing wrong?
SELECT
posts.id as post_id
,posts.text
,posts.name
,posts.photo
,posts.hash as post_hash
,posts.date_added as post_date_added
,users.id as usr_id
,users.uname
,users.avatar
,users.sex
,users.hash as usr_hash
,COUNT(comments.id) AS comment_count
FROM
followers
LEFT OUTER JOIN users
ON users.id = followers.following
LEFT OUTER JOIN posts
ON posts.usr_id = followers.following
LEFT OUTER JOIN comments
ON comments.post_id = posts.id
WHERE
followers.user=1
UNION
SELECT
posts.id as post_id
,posts.text
,posts.name
,posts.photo
,posts.hash as post_hash
,posts.date_added as post_date_added
,users.id as usr_id
,users.uname
,users.avatar
,users.sex
,users.hash as usr_hash
,COUNT(comments.id) AS comment_count
FROM
followers
LEFT OUTER JOIN users
ON users.id=1
LEFT OUTER JOIN posts
ON posts.usr_id=1
LEFT OUTER JOIN comments
ON comments.post_id = posts.id
ORDER BY
post_date_added
LIMIT 10
I spoke too soon. The second subselect is returning the wrong number of comments for user 1
SELECT
posts.id as post_id
,posts.text
,posts.name
,posts.photo
,posts.hash as post_hash
,posts.date_added as post_date_added
,users.id as usr_id
,users.uname
,users.avatar
,users.sex
,users.hash as usr_hash
,COUNT(comments.id) AS comment_count
FROM
followers
LEFT OUTER JOIN users
ON users.id = followers.following
LEFT OUTER JOIN posts
ON posts.usr_id = followers.following
LEFT OUTER JOIN comments
ON comments.post_id = posts.id
WHERE
followers.user=1
GROUP BY
posts.id
UNION
SELECT
posts.id as post_id
,posts.text
,posts.name
,posts.photo
,posts.hash as post_hash
,posts.date_added as post_date_added
,users.id as usr_id
,users.uname
,users.avatar
,users.sex
,users.hash as usr_hash
,COUNT(comments.id) AS comment_count
FROM
followers
LEFT OUTER JOIN users
ON users.id=1
LEFT OUTER JOIN posts
ON posts.usr_id=1
LEFT OUTER JOIN comments
ON comments.post_id = posts.id
GROUP BY
posts.id
ORDER BY
post_id DESC
LIMIT 10
Good tips, thanks. I was able to refine my queries by breaking them up like that and going over and over this until my eyes bled.
Still having a problem with COUNT. The two queries produce the correct results when run independently AND when concatenated with a UNION. However, when I thrown a COUNT in for the comments table all hell breaks loose again and I get weird results.
SELECT
posts.id as post_id
,posts.text
,posts.name
,posts.photo
,posts.hash as post_hash
,posts.date_added as post_date_added
,users.id as usr_id
,users.uname
,users.avatar
,users.sex
,users.hash as usr_hash
,comments.comment
FROM
followers
LEFT OUTER JOIN users
ON users.id = followers.following
LEFT OUTER JOIN posts
ON posts.usr_id = followers.following
LEFT OUTER JOIN comments
ON comments.post_id = posts.id
WHERE
followers.user=1
SELECT
posts.id as post_id
,posts.text
,posts.name
,posts.photo
,posts.hash as post_hash
,posts.date_added as post_date_added
,users.id as usr_id
,users.uname
,users.avatar
,users.sex
,users.hash as usr_hash
,comments.comment
FROM
posts
LEFT OUTER JOIN users
ON users.id=1
LEFT OUTER JOIN comments
ON comments.post_id = posts.id
WHERE
posts.usr_id=1
ORDER BY
post_id DESC