dow57588 2018-10-16 03:19
浏览 61

Mysqli选择具有相同id和sum的多行然后在PHP中返回

I am having trouble with return sum data in MYSQLI and PHP. Here is my database. Database

For example,i choose user_id = '0', and i need to sum the number in m_debit and deduct m_credit for user_id = '0' to get the final total. The result should be like this:

The total should be appear in Metallic Point column The total should be appear in Metallic Point column

here is my current coding:

$get_member = "select sum (m_debit) from transaction_record_tpg where user_id = 0 ";
$run_customer = mysqli_query($conn,$get_member);
$a = mysqli_num_rows($run_customer);

can you guys help me to get the result?

[Updated] Below is my new coding but it doesnt work

<?php
$get_member = "SELECT SUM(m_debit) - SUM(m_credit) AS   metallic_point FROM transaction_record_tpg WHERE user_id = 0";
$run_customer = mysqli_query($conn,$get_member);
$row = mysqli_fetch_row($run_customer);
?>

<tr>

<td><?php echo $row; ?></td>
<td>2018-10-15 11:03:13</td>
<td>TMA</td>
<td>2018-12-15</td>

</tr><!-- tr Ends -->
  • 写回答

2条回答 默认 最新

  • drasebt1835 2018-10-16 03:27
    关注

    Sum both columns and then subtract one of them to deduct it.

    SELECT SUM(m_debit) - SUM(m_credit) AS metallic_point
    FROM transaction_record_tpg
    WHERE user_id = 0
    

    There's no point in calling mysqli_num_rows(). It will always be 1 for this query. Just fetch the row and use $row['metallic_point'] as the value.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?