0

我被这个困住了。

我的数据库中有两个表(mysql)1是(i_mst),2是(o_mst)

我在 i_mst 中有日期列,在 o_mst 中有金额列,在 o_mst 中有 created_by 列。

我需要的是

distinct created_by and sum each created_by amount By i_mst date 

    BETWEEN '2013-01-01'
    AND '2013-08-01'

并在单行中返回。

为此,我正在尝试这个。

SELECT DISTINCT (
o_mst.created_by
), o_mst.amount
FROM o_mst
LEFT JOIN i_mst ON i_mst.i_date
WHERE i_date
BETWEEN '2013-01-01'
AND '2013-08-01'
ORDER BY `o_mst`.`amount` DESC
LIMIT 0 , 5

它给了我多个 created_by id 和它们的数量。

我想获得不同的 created_by 行并为每个 created_by 行求和 total_amount。

如果可能,请帮助我。

谢谢

4

3 回答 3

0

你可以试试这个查询

SELECT o_mst.created_by, SUM(o_mst.amount)
FROM o_mst
LEFT JOIN i_mst ON i_mst.i_date
WHERE i_date
BETWEEN '2013-01-01'
AND '2013-08-01'
GROUP BY o_mst.created_by
ORDER BY `o_mst`.`amount` DESC
LIMIT 0 , 5
于 2013-07-18T07:58:40.217 回答
0

试试这个

SELECT o_mst.created_by, SUM(o_mst.amount)
FROM o_mst
INNER JOIN i_mst ON i_mst.inq_mst_id = o_mst.inq_mst_id 
WHERE i_date
BETWEEN '2013-01-01'
AND '2013-08-01'
GROUP BY o_mst.created_by DESC
LIMIT 0 , 5
于 2013-07-18T08:44:42.120 回答
0

这个怎么样

SELECT DISTINCT_VALUES.created_by, 
       SUM(DISTINCT_VALUES.amount)
 FROM
    (

   SELECT * 
   FROM o_mst
   LEFT JOIN i_mst ON i_mst.i_date
   WHERE i_date
   BETWEEN '2013-01-01'
   AND '2013-08-01'
    GROUP BY o_mst.created_by,  //.. all the columns that you want to merge
   ORDER BY `o_mst`.`amount` DESC
    LIMIT 0 , 5
) DISTINCT_VALUES
于 2013-07-19T09:07:53.087 回答