1

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)'];
    }

3 Answers 3

1

try:

 select name, avg(Q1) as Q1_Average from record where name='Alex' group by name
Sign up to request clarification or add additional context in comments.

Comments

0

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'];

Comments

0

Use group by clause to find average of indivisuals:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.