SELECT pc.com_comment
, u.usr_avatar
, u.usr_uname
, CASE WHEN pv.vote_post_id IS NULL
THEN 'no'
ELSE 'yes' END AS voted
FROM posts_comments AS pc
INNER
JOIN users AS u
ON u.usr_id = pc.com_usr_id
LEFT OUTER
JOIN posts_votes AS pv
ON pv.vote_post_id = pc.com_post_id
AND pv.vote_usr_id = pc.com_usr_id
WHERE pc.com_post_id = 13
ORDER
BY pc.com_id DESC
I’m just looking for a result so I know the user voted for this post. I just picked a name so something would be returned, as a boolean, basically.
I want the user’s comment (com_comment), their avatar (usr_avatar), username (usr_uname) and to know if they voted or not. If a value is in the posts_votes table that contains the post’s ID and the user’s ID, then I know they voted.
why would you combine SELECT vote_usr_id with WHERE vote_usr_id = 2? you’re only going to get the answer “2”, i.e. exactly what you fed in to the query
when you say you want “a result”, which columns, specifically, did you want the combined query to return?
Okay, I’ve realized that I forgot to tell you that I need to have an ORDER BY in there so that the most recent comment or vote is at the top of the list (Sorry!). I’ve Googled how to do this and, as you already know, it ain’t easy! I couldn’t figure it out.
Does this look correct? It’s returning results… it’s just hard to check if they’re correct because I don’t have any dates in the pc.com_date_added table.
SELECT
u.usr_avatar,
u.usr_uname,
pc.com_comment,
pc.com_date_added
FROM posts_comments AS pc
INNER
JOIN users AS u
ON u.usr_id = pc.com_usr_id
WHERE pc.com_post_id = 13
UNION ALL
SELECT
u.usr_avatar,
u.usr_uname,
'voted',
pv.vote_date_added
FROM posts_votes AS pv
INNER
JOIN users AS u
ON u.usr_id = pv.vote_usr_id
WHERE pv.vote_post_id = 13
ORDER BY com_date_added DESC