Jump to content

Recommended Posts

I have the following query:

 

SELECT *, c.uniq_id as cid, c.name as cname, c.date as cdate 
FROM comments c 
LEFT JOIN users u 
ON (c.owner = u.id) 
LEFT JOIN like_dislike_comment l
ON (l.commentID = c.uniq_id)
WHERE c.id = '$id' 
GROUP BY l.commentID
ORDER BY 
c.uniq_id DESC

 

The above query gets all comments for a particular page, each comment also has a like/dislike button 1 = like; 2 = dislike. for each comment (row) I want to have a count of the number of likes, and the number of dislikes of each comment. I then want to have the one with the MOST likes first, and after that in original order of post date. How would I write something like that?

 

Hope this makes sense.

Link to comment
https://forums.phpfreaks.com/topic/211534-counting-and-ordering/
Share on other sites

Hi

 

Think something like this would do it for you (not tested)

 

SELECT *, c.uniq_id as cid, c.name as cname, c.date as cdate, z.DislikeCount, y.LikeCount
FROM comments c
LEFT JOIN users u ON (c.owner = u.id)
LEFT JOIN like_dislike_comment l ON (l.commentID = c.uniq_id)
LEFT OUTER JOIN (SELECT commentID, COUNT(*) AS LikeCount
FROM like_dislike_comment 
WHERE LikeDislike = 1
GROUP BY c.uniq_id) y
LEFT OUTER JOIN (SELECT commentID, COUNT(*) AS DislikeCount
FROM like_dislike_comment 
WHERE LikeDislike = 2
GROUP BY c.uniq_id) z
WHERE c.id = '$id'
GROUP BY l.commentID
ORDER BY y.LikeCount DESC, c.Date

 

All the best

 

Keith

I am getting this error:

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE c.uniq_id = '$id'

GROUP BY l.commentID

ORDER BY y.LikeCount DESC, c.Date

L' at line 17

 

I am replacing $id with 29 when I run the code.

Hi

 

Doh, missed off the ON clauses:-

 

SELECT *, c.uniq_id as cid, c.name as cname, c.date as cdate, z.DislikeCount, y.LikeCount
FROM comments c
LEFT JOIN users u ON (c.owner = u.id)
LEFT JOIN like_dislike_comment l ON (l.commentID = c.uniq_id)
LEFT OUTER JOIN (SELECT commentID, COUNT(*) AS LikeCount
FROM like_dislike_comment 
WHERE LikeDislike = 1
GROUP BY c.uniq_id) y ON c.uniq_id = y.commentID
LEFT OUTER JOIN (SELECT commentID, COUNT(*) AS DislikeCount
FROM like_dislike_comment 
WHERE LikeDislike = 2
GROUP BY c.uniq_id) z ON c.uniq_id = z.commentID
WHERE c.id = '3'
GROUP BY l.commentID
ORDER BY y.LikeCount DESC, c.Date 

 

All the best

 

Keith

Hi

 

Long day..........

 

 

SELECT *, c.uniq_id as cid, c.name as cname, c.date as cdate, z.DislikeCount, y.LikeCount
FROM comments c
LEFT JOIN users u ON (c.owner = u.id)
LEFT JOIN like_dislike_comment l ON (l.commentID = c.uniq_id)
LEFT OUTER JOIN (SELECT commentID, COUNT(*) AS LikeCount
FROM like_dislike_comment 
WHERE LikeDislike = 1
GROUP BY commentID) y ON c.uniq_id = y.commentID
LEFT OUTER JOIN (SELECT commentID, COUNT(*) AS DislikeCount
FROM like_dislike_comment 
WHERE LikeDislike = 2
GROUP BY commentID) z ON c.uniq_id = z.commentID
WHERE c.id = '3'
GROUP BY l.commentID
ORDER BY y.LikeCount DESC, c.Date 

 

When I first knocked the code up I did have a JOIN in the sub selects and then realised it wasn't needed so took it out. But forgot to change the column name in the group by.

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.