如何在php中求mysql表列的和?

8hhllhi2  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(278)

我想得到数据库中表列的总和。我尝试过使用以下代码,但没有成功。

$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);
$result = mysqli_query($link,'SELECT SUM(value) AS value_sum FROM  User_Table'); 
$row = mysqli_fetch_assoc($result); 
$sum = $row['value_sum'];
echo $sum;

非常感谢你!!

vnzz0bqm

vnzz0bqm1#

我希望你能找到一个用户表的记录总数

$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);
$result = mysqli_query($link,'SELECT SUM(your_column_name) AS value_sum FROM  User_Table'); 
//or like the query for return last row that indicate total number of record 
// id auto increment
$result = mysqli_query($link,'SELECT * FROM User_Table ORDER BY id DESC LIMIT 1;'); 
$row = mysqli_fetch_assoc($result); 
$sum = $row['id'];
echo $sum;  

// or using count
    $result = mysqli_query($link,'SELECT COUNT(*) total_row FROM User_Table;'); 
    $row = mysqli_fetch_assoc($result); 
    $sum = $row['total_row '];
    echo $sum;
azpvetkf

azpvetkf2#

据我所知,您需要计算数据库的列数。如果我没有错,你可以使用下面的查询

select * from information_schema.columns
where table_schema = '<YOUR DATABASE NAME>'
order by table_name,ordinal_position

希望这有帮助。谢谢

相关问题