Jump to content

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

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.