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
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
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
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.