I have three tables this is my codes:
<table border='1'>
<tr>
<th>Subject</th>
<th>Mark1</th>
<th>Mark2</th>
<th>Mark3</th>
<th>Sum Marks</th>
</tr>
<?php
$query2 = "SELECT students_info.s_name, subjects.subject, marks.*
FROM subjects
RIGHT JOIN (students_info
RIGHT JOIN marks ON students_info.s_id = marks.s_id
) ON subjects.sub_id = marks.sub_id
WHERE students_info.s_id = '".$s_id."'";
$result2=mysqli_query($link,$query2) OR die(mysqli_error($link));
while ($row=mysqli_fetch_array($result2))
{
?>
<tr>
<td><?php echo $row['subject'];?></td>
<td><?php echo $row['n1'];?></td>
<td><?php echo $row['n2'];?></td>
<td><?php echo $row['n3'];?></td>
<td><?php echo $row['n1']+$row['n2']+$row['n3'];?></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
Output my codes:
I want when each mark form(mark1,mark2,mark3) for every subject less than 49 the sum operation not work in the cell sum marks appear empty value or empty cell, Please review my code and give me a help, to do work this code.
Rick James
80.7k5 gold badges52 silver badges119 bronze badges
1 Answer 1
Could add them up in SQL by reverting to 0 for NULL columns like:
SELECT students_info.s_name, subjects.subject, marks.*,
COALESCE(marks.n1, 0) + COALESCE(marks.n2, 0) +COALESCE(marks.n3, 0) as sum_marks
FROM ....
answered Aug 30, 2018 at 23:40
-
Thanks for your answer @danblack, i used your code but the same output appear, i want to when each mark less than(<49) the cell Sum Marks must be empty for this rowTalib– Talib2018年08月31日 20:45:00 +00:00Commented Aug 31, 2018 at 20:45
-
-
I used this criteria, but this way save only makr3 :( .Talib– Talib2018年09月03日 20:02:46 +00:00Commented Sep 3, 2018 at 20:02
-
1
IF( marks.n1 < 49 AND marks.n2 < 49 AND marks.n3 < 49, NULL, COALESCE(marks.n1, 0) + COALESCE(marks.n2, 0) +COALESCE(marks.n3, 0))
danblack– danblack2018年09月03日 21:22:17 +00:00Commented Sep 3, 2018 at 21:22 -
Thanks @danblack, that's worked for me, but if the name subjects on the header name students on the left and marks on the center how this code work for sum?Talib– Talib2018年09月04日 08:20:14 +00:00Commented Sep 4, 2018 at 8:20
lang-sql