Jump to content

Counting and ordering


The Little Guy

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.