mysqli选择多个具有相同id和sum的行,然后在php中返回

hpcdzsge  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(218)

我在mysqli和php中遇到返回和数据的问题。这是我的数据库。

例如,我选择user\u id='0',我需要对m\u debit中的数字求和,并为user\u id='0'扣除m\u credit以得到最终的总数。结果应该是这样的:
总数应出现在金属点列中

这是我目前的编码:

$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);

你们能帮我得到结果吗?
[更新]下面是我的新代码,但它不工作

<?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 -->
pcrecxhr

pcrecxhr1#

$get_member = "SELECT SUM(m_debit) - SUM(m_credit) AS metallic_point FROM transaction_record_tpg WHERE user_id = 0";
$result = mysqli_query($conn, $get_member);
$data = mysqli_fetch_array($result);

echo $data['metallic_point'];
k2arahey

k2arahey2#

对两列求和,然后减去其中一列即可将其扣除。

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

打电话没有意义 mysqli_num_rows() . 此查询将始终为1。把那行拿来用 $row['metallic_point'] 作为值。

相关问题