Jump to content

[SOLVED] SQL help


play_

Recommended Posts

I have a table that looks similar to this:

 

|userID | fname | lname | school |

| 1      | John  | Doe  |  UMD  |

--------|-------|------|--------|

| 2      | Janet  | Smith| UCLA  |

--------|-------|------|--------|

 

Just exmaple entries. It's going to get alot larger than that.

 

User selects school from a drop-down menu.

What i'd like to do is, retrieve all schools and list by popularity. How can I do that?

Link to comment
https://forums.phpfreaks.com/topic/76637-solved-sql-help/
Share on other sites

@ Gingerroot

Where does that data (e.g. the number) come from?

 

The number of times it is in the database.

 

So say I have 10 users who signed up.

 

2 of them go to UMD.

3 of them go toVT

4 of them go to UCLA

1 goes to WVU

 

I'd then like to list the results from highest, to lowest. Like this:

UCLA: 4

VA  : 3

UMD : 2

WVU : 1

Link to comment
https://forums.phpfreaks.com/topic/76637-solved-sql-help/#findComment-388041
Share on other sites

Ah, im with you. Try:

 

<?php
$sql = "SELECT COUNT(*) as `number`,`school` FROM `yourtable` GROUP BY `school` ORDER BY `number` DESC";
$result = mysql_query($sql) or die(mysql_error());
while(list($number,$school) = mysql_fetch_row($result)){
echo $school.' : '.$number.'<br />';
}
?>

 

Edit: Ideally you should be storing a school ID in this table, and have a separate table of school names.

Link to comment
https://forums.phpfreaks.com/topic/76637-solved-sql-help/#findComment-388046
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.