Andrew
Andrew

Reputation: 53

PHP - average column where another column is a specific name

In PHP How can I average a column where another column is a specific name. There will be several records for the same person in this table. I don't get any result here.

Eg:

 $result = mysqli_query($con,"Select * FROM record WHERE Name='Alex'");
 $avg = "SELECT Name='Alex', AVG(Q1) FROM record";
 $average = mysqli_query($avg);

 while($row = mysqli_fetch_array($average)){
 echo $row['AVG(Q1)'];
    }

Upvotes: 1

Views: 83

Answers (3)

Jelle Ferwerda
Jelle Ferwerda

Reputation: 1229

try:

 select name, avg(Q1) as Q1_Average from record where name='Alex' group by name

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53525

What you need is to change your query to:

SELECT NAME, AVG(Q1) as average FROM record where NAME='Alex' group by NAME

and then when you try to read the result:

echo $row['average'];

Upvotes: 0

Joke_Sense10
Joke_Sense10

Reputation: 5402

Use group by clause to find average of indivisuals:

$avg = "SELECT Name,AVG(Q1) FROM record group by Name";

Upvotes: 0

Related Questions