Jump to content

Sorting Records


Alicia

Recommended Posts

hi guys, can somebody give me an idea how I sort the records that I want to display order by value that I sum up from records?

 

I have a query like this :

// in a while loop

$sold = mysql_query("SELECT COUNT( class ) AS num FROM `Student` WHERE class ='xxx' ");

 

Anyway I can do something in a new query like

select * from class order by num ASC ?

 

first i want to count how many students in every class from Student table

 

then I want to arrange the class records and display in a web page order by number of students they have in each of the classes. I am not able to sort from the class table because there is no column for this value, thats why I need to count from another table first.

TQ

Link to comment
https://forums.phpfreaks.com/topic/269258-sorting-records/
Share on other sites

I'm guessing that you finally need the information from the "class" table, is that is true only one query using a JOIN between "class" and "students" should be enough

 

SELECT class.*, count(students.class) AS ngroup // if you don't need ALL the columns of the guess table don't use "*" and name the columns that you need
FROM class
LEFT JOIN students ON students.class = class.id // assuming that "class.id" is the same column as students.class.
GROUP BY class.id // assuming that "id" is your class table PK
ORDER BY ngroup ASC

Link to comment
https://forums.phpfreaks.com/topic/269258-sorting-records/#findComment-1383954
Share on other sites

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.