gpt4 book ai didi

php - Mysql - PHP - 查看总和列

转载 作者:行者123 更新时间:2023-11-29 06:26:59 24 4
gpt4 key购买 nike

我如何查看 (Collections) 列的总和,其中 (userid = $userid)?

示例:我需要查看用户 id = 2,集合 = 200 + 330 = 530 的结果,我需要查看此结果 (530)

我的 table

------------------------------
| id | user_id | Collections |
------------------------------
| 1 | 2 | 200 |
------------------------------
| 2 | 2 | 330 |
------------------------------
| 3 | 7 | 120 |
------------------------------
| 4 | 8 | 760 |
------------------------------
| 5 | 9 | 200 |
------------------------------
| 6 | 9 | 100 |
------------------------------

我的代码

<?php

$user_id = get_current_user_id();

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$query = "SELECT SUM(Collections) FROM invoices where user_id = $user_id";

$result = mysql_query($query) or die(mysql_error());



// Print out result

?>
  • 我是 php 和 mysql 的初学者

最佳答案

你可以试试这个:

SELECT SUM(i.Collections) AS totalCollection
FROM invoices AS i
WHERE i.user_id = '$user_id'
GROUP BY i.user_id

关于php - Mysql - PHP - 查看总和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30409641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com