doumianfeng6979 2015-02-18 11:55
浏览 13

表中的和数并保存在变量中

I have table money in first row I have number 10 in second 20 and third 30 and now I'm going to sum this rows and save into variable $sum

This is my code

$result = mysql_query("SELECT SUM(money) as total FROM $tbl_name WHERE id='$idd'");
$row= mysql_fetch_array($result);
$sum= $row['total'];

If I print $sum Result is 102030 but I need to print 60

  • 写回答

1条回答 默认 最新

  • douren0558 2015-02-18 12:16
    关注

    First of all, you have to change your table's structure.

    alter table $tablename alter column money decimal(10,2)
    

    Choose the decimal precision that fits your needs.

    If you are really stuck because you do not have access to change the table structure, you could change your query to :

    SELECT SUM(cast(money as decimal(10,2))) as total FROM $tbl_name WHERE id='$idd'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析