寻找最大收入日

时间:2012-06-08 15:15:28

标签: php mysql

我的表收入包含以下字段

+----+-----------+-------------+---------------+
| id | member_id | amount_paid | datetime_paid |
+----+-----------+-------------+---------------+

我想运行查询以查找前10个最长收入天数。实现这一目标的最快方法是什么?该表有很多数据。

此致

3 个答案:

答案 0 :(得分:4)

尝试执行以下操作的基本查询:

SELECT DATE(`datetime_paid`) AS `Max Revenue`
    FROM `revenue`
    ORDER BY `amount_paid` DESC
    GROUP BY DATE(`datetime_paid`)
    LIMIT 10;

这将显示十大最高薪酬天数。 :)

答案 1 :(得分:2)

假设您的datetime_paid列具有某种日期或时间类型(see MySQL docu),则以下内容应该有效。

SELECT SUM( `amount_paid` ) AS `amount`, DATE( `datetime_paid` ) as `date`
  FROM yourTable
  GROUP BY `date`
  ORDER BY `amount` DESC
  LIMIT 10

答案 2 :(得分:0)

首先,您需要在付款之日对结果进行分组。这可以通过选择DATE(datetime_paid) as payday,然后按payday分组来完成。要获得总数,请选择SUM(amount_paid) as total。最后,要获得前10名,请按total desclimit 10订购。

您的最终查询应如下所示:

SELECT DATE(`datetime_paid`) AS `payday`, SUM(`amount_paid`) AS `total`
FROM `table_name_here`
GROUP BY `payday`
ORDER BY `total` DESC
LIMIT 10

您可能需要将ORDER BY行更改为ORDER BY SUM(amount_paid) DESC,我不记得订购时是否允许使用字段别名。