来自sqlite中多个表的多个查询

时间:2012-09-05 11:59:31

标签: sqlite sum

我正在使用它,但我发现没有价值......我认为这个查询中有错误....实际上我想知道如何在sqlite中使用多个表来使用多个和,乘法等

SELECT 
  dhid, dprice, dname,
  SUM(dmilk) AS totalmilk,
  dprice*SUM(dmilk) AS totalmilkamt,
  SUM(ghee) AS toalghee,
  SUM(ghee*gheeprice) AS totalgheeamt,
  SUM(ghee*gheeprice)+dprice*SUM(dmilk)  AS totals,
  SUM(cashamount)   AS totalcash,
  SUM(ghee*gheeprice)+dprice*SUM(dmilk)-SUM(cashamount) AS balance 
FROM
  (         SELECT *
              FROM costumer
   LEFT OUTER JOIN salesdata
                ON costumer.dhid=salesdata.ddhid
   LEFT OUTER JOIN cashdata
                ON salesdata.ddhid=cashdata.uid
               AND utype='costumer')
   WHERE dmonth='$mikdatem'
     AND dyear='$mikdatey' 
     AND dhid='$dhid' 
ORDER BY dhid ASC

1 个答案:

答案 0 :(得分:0)

您上面的选择对我们没有帮助,因为我们没有基础数据来了解您的目标。

所以一般的答案是这样的: 当使用分组函数(SUM / COUNT ...)时,您总是需要某种形式的“GROUP BY”到未在这些组函数中使用的列。

给出的例子:

SELECT name, sum(dmilk)
FROM milk_entry
GROUP BY name