duandai6373 2014-12-10 03:44
浏览 96

mysql sum函数没有给出结果

I am a bit confused in calculating sum of particular column using mysql.

Database Structure table name: personal there is 25 rows in total, and columns id, userid, name and amount.

I want to calculate amount of particular userid through running session.

Code:

<?php
$userid = $_SESSION['userid'];
$sql = "SELECT userid, SUM(amount) FROM personal";
echo $sql."<br/>";
echo $userid;
?>

Output: SELECT userid, SUM(amount) FROM personal gold99

Please help..guys..

  • 写回答

4条回答 默认 最新

  • doutuo6689 2014-12-10 03:53
    关注

    As you have written it you will get one arbitrary user name, followed by the sum of all amounts. If you want the sum for each user name you need to use SUM as an aggregate function with a GROUP BY:

    SELECT userid, sum(amount) FROM personal GROUP BY userid
    

    and as mentioned if you want the sum for just one user, use a WHERE clause.
    But getting the query right is only part of the problem. You also need to use php functions to execute that query against some database. This is normally done either with mysqli_ APIs or PDO (more object-y and works against various database providers). You'll need to access a database, create a statement, execute it, and fetch the results. See http://php.net/manual/en/book.mysqli.php and http://php.net/manual/en/book.pdo.php for more, and give it a try on your own.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀