Jw13
Jw13

Reputation: 9

Finding and printing most common value within MySql database using PHP

I currently have the code to retrieve values, but I don't know how to return the modal value in this array.

$stuff = "SELECT COUNT (SubjectNo) FROM SubjectAllocation";
$direct = mysqli_query($link, $stuff);

while($row = mysqli_fetch_array($direct)){
    echo $row['COUNT (SubjectNo)'];
    echo "<br />";
}

I understand this is just performing a count, but I want to define a base to start from.

Upvotes: 1

Views: 47

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271241

Here is one method for getting the "mode" -- the statistical term for the most common value:

select subjectno
from subjectallocation
group by subjectno
order by count(*) desc
limit 1;

Upvotes: 3

Related Questions