How can I group mysql results based on a calculated colum?

kukiko11

I'm trying to show the most commented posts of my web but is being impossible .-. I just got this mysql error all the time (Can't group on 'comments')

the relation between tables is:

table: post / colums: id_post, title, id_comment
table : comment / colums: id_comment, text, id_post

and this is the query I'm trying to use

SELECT p.title AS title, COUNT(c.id_comment) AS comments
FROM post p
INNER JOIN comment c ON p.id_post=c.id_post
GROUP BY comments DESC

Please any alternative or solution for this?

Shadow

Why would you want to group by on the number of comments? You need order by clause to get the most commented to the top and group by posts:

SELECT p.title AS title, COUNT(c.id_comment) AS comments
FROM post p
INNER JOIN comment c ON p.id_post=c.id_post
GROUP BY p.id_post, p.title
ORDER BY comments DESC

You may want to have a limit clause as well to get the top N commented posts only.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i convert individual days into one date colum of Pandas

How I can group the results by day in this query with SQL Server?

How can I detect if a colum contain constatnt values

How can I filter results based on multiple selections from dropdown?

how can I group based on similarity in strings

How can I get the count of different values of one colum

How can I join two tables in teradata in which the output columns are calculated based on a condition?

In Tableau how can I create a calculated field that categorizes data based on data across rows?

How can I sum a group of a custom calculated field in SQL?

SQL how can I skip repeated values on a colum

How can I add a custom method to auth_user model class? I need to return a calculated field along with results

How do I group a colum sort in Excel by year and then by month?

How can I get the sum of a colum for each dynamically generated table?

How can I get the unix group name based on the group id?

How can I add a calculated column based on another column in an SQL select statement

Group and count data based on a calculated field in mysql?

How can I use a subquery on my query results and then ORDER BY a calculated result for each row?

How can I let div inside bootstrap colum scrollable

How can I use group membership to restrict results?

How to group mysql query results based on a avg calculation?

Order php/mysql results based on google api calculated distance

How can I group MySQL Workbench connections?

How can I automatically detect if a colum is categorical?

How can I select all rows based on the AVG calculated in another column?

How to make php get last row from a Colum from mysql and make the result a variable that I can take with javascript?

How can I fetch 0 results from missing row in group by

How do I store a list of int in one mysql colum?

How can I select unique results from a table based on a column?

How can I group API results by an id in a dataframe