Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

php - How can I query total pay as student id?

Query as student id and show it as total pay. I was post before about this problem. maybe I was not able to clear my question.so I post it again. please help me

http://i.stack.imgur.com/SuYwb.jpg

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

?>


<?php
$sql0="SELECT * FROM payment as std_id GROUP BY std_id ";
$sql="SELECT SUM(pay) FROM payment as std_id GROUP BY std_id ";
$result0=mysql_query($sql0);
$result=mysql_query($sql);
?>






<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="6"><div align="center"><strong>List data from mysql </strong> </div></td>
</tr>

<tr>
<td align="center"><strong>std id</strong></td>
<td align="center"><strong>pay</strong></td>



<?php
while($rows0=mysql_fetch_array($result0))  {
?>

<tr>
<?php
while($rows=mysql_fetch_array($result))  {
?>

<td><?php echo $rows0['std_id']; ?></td>
<td><?php echo $rows['SUM(pay)']; ?></td>



</tr>

<?php
}
}
?>



</table>
</td>
</tr>
</table>




<?php
mysql_close();
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

Here you need to get amount Student wise, so need to do GROUP BY student & also want sum amount, so need to call SUM function and pass amount in it.

Here is MySQL Query:

SELECT std_id AS studentid , SUM(pay) as Amount 
FROM table 
GROUP BY std_id;

Let me know if you need further help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...