dtlc84438 2014-12-26 10:13
浏览 6
已采纳

如何在sum [closed]中显示表中的2tabile数据

I want to show some data from 2 table to 1 table. and 1 table's column will be sum as student id.

finally all data will show as student id please see this image for clear my question. PLS help.

enter image description here

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="workshop_all"; // 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


$sqlnew="SELECT std_id, sum(pay) AS pay FROM payment GROUP BY std_id";
$resultnew=mysql_query($sqlnew);



?>






<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>Email</strong></td>
<td align="center"><strong>pay</strong></td>




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

<td><?php echo $rows['std_id']; ?></td>
<td><?php echo $rows['email']; ?></td>
<td><?php echo $rows['pay']; ?></td>



</tr>

<?php
}

?>



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




<?php
mysql_close();
?>

</div>
  • 写回答

3条回答 默认 最新

  • douliu3831 2014-12-26 10:19
    关注

    Used the join in query like bellow

    //change TABLE1 TO your table name
    
    $sqlnew="SELECT p.std_id, sum(p.pay) AS pay,t1.email FROM payment p LEFT JOIN TABLE1 as t1 ON(t1.id=p.std_id) GROUP BY p.std_id";
    $resultnew=mysql_query($sqlnew);
    

    change the join type as per your requirement. the above query for only example

    • INNER JOIN: Returns all rows when there is at least one match in BOTH tables

    • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table

    • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table

    • FULL JOIN: Return all rows when there is a match in ONE of the tables

    more about join click here AND also check this http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条