Search code examples
mysqlsqlsortingrating

mySQL - Sorting techniques and ordering


i have a table that contains a list of images and would like to sort them to a popular image sequence. Below listed the table and i would like to make the "Target column", in order for me to sort popular image.

sql_table

Here is the SQL code:

SELECT imageID FROM  `ratings` WHERE rating > 3 ORDER BY imageID DESC

Thanks for your help.


Solution

  • Try this, use a count and a group by, this will count repeated ImageID:

    SELECT count(*) as ct, imageID  
      FROM `ratings` 
     WHERE rating > 3 
     GROUP BY imageID
     ORDER BY ct