如何排序和添加我的数据库记录? [英] How to sort and add my database record?

查看:75
本文介绍了如何排序和添加我的数据库记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何按数据库中的先前日期排序?

Possible Duplicate:
How to sort by previous date in database?

我要做的是从以前的日期记录的mysql中选择,然后从数据库记录中添加按日期排序的量

I want to do is select from mysql which is previous date records then from my database records I need to add the amount which is sort by date

我的数据库记录:

amount | date
2.0    | 2011-10-01
1.4    | 2011-10-02
2.6    | 2011-10-01
2.3    | 2011-10-02
4.4    | 2011-10-01
5.6    | 2011-10-03
6.3    | 2011-10-01

我要在界面中显示的内容

What I want to display in my interface:

amount | date
2.0    | 2011-10-01
2.6    | 2011-10-01
4.4    | 2011-10-01
6.3    | 2011-10-01
---------------------------
15.3   | 2011-10-01


1.4    | 2011-10-02  
2.3    | 2011-10-02
----------------------------
3.7    | 2011-10-02


5.6    | 2011-10-03
----------------------------
5.6    | 2011-10-03

我的代码(不起作用):

my code (it not work):

$Current = date("Y-m-d" ,strtotime("now"));
$query=mysql_query("SELECT * from item where date < $Current order by date");     
    while($re=mysql_fetch_array($query)){
    echo $total = $total + $re['amount'];
    echo $re['date'];
    }

这不是很好吗?

谢谢

推荐答案

您是否研究过MySQL的

Have you looked into MySQL's WITH ROLLUP option.

类似这样的东西

SELECT date,amount,item_id FROM table GROUP BY date,item_id WITH ROLLUP

这篇关于如何排序和添加我的数据库记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆