尝试连接两个表时Sum不正确

时间:2015-04-17 12:15:44

标签: php mysql sql codeigniter sum

我在使用联接时努力从qty列获取正确的总和。当我尝试从timestamp表中获取paymentType并将rowid加入orderspaymentType表,然后将timestamp分组时,会出现此问题{1}}

天{1}}

我希望使用(day(from_unixtime(paymentType.timestamp)))表格中的qty按小时计算timestamp的总和,唯一的链接是paymentType(这是codeigniter' s购物车模块的rowid)。逻辑问题(至少对我而言)是rowid表中存在的行数(因为这是每个产品)比orders表中存在的更多行(这只是为了跟踪借方还是现金)用过)。当我将这些表连接在一起时,每小时的总和乘以paymentType中的每次匹配。

如果解释不好,我很抱歉,但我希望我能够理解这件事可以得到帮助。

我至少有10次查询,但似乎都没有按照我想要的方式运作。

以下是我的orders.rowid <--> paymentType.rowid表格

orders

这是+---------+----+-------+-----+----------+------------------+----------------------------------+ | orderID | id | price | qty | subtotal | name | rowid | +---------+----+-------+-----+----------+------------------+----------------------------------+ | 3 | 49 | 35 | 1 | 35 | Red Bull Stor | f457c545a9ded88f18ecee47145a72c0 | | 4 | 24 | 35 | 1 | 35 | Monster Energy | 1ff1de774005f8da13f42943881c655f | | 5 | 49 | 35 | 1 | 35 | Red Bull Stor | f457c545a9ded88f18ecee47145a72c0 | | 6 | 19 | 20 | 1 | 20 | Sprite 0.5L | 1f0e3dad99908345f7439f8ffabdffc4 | | 7 | 1 | 25 | 1 | 25 | Pringles | c4ca4238a0b923820dcc509a6f75849b | | 8 | 43 | 20 | 1 | 20 | Lån av stekovn | 17e62166fc8586dfa4d1bc0e1742c08b | | 9 | 46 | 35 | 1 | 35 | Burn | d9d4f495e875a2e075a1a4a6e1b9770f | | 10 | 49 | 35 | 3 | 105 | Red Bull Stor | f457c545a9ded88f18ecee47145a72c0 | | 11 | 49 | 35 | 1 | 35 | Red Bull Stor | f457c545a9ded88f18ecee47145a72c0 | | 12 | 29 | 25 | 1 | 25 | Potetskruer | 6ea9ab1baa0efb9e19094440c317e21b | | 13 | 16 | 20 | 1 | 20 | Coca-Cola 0.5L | c74d97b01eae257e44aa9d5bade97baf | | 14 | 46 | 35 | 1 | 35 | Burn | d9d4f495e875a2e075a1a4a6e1b9770f | | 15 | 1 | 25 | 1 | 25 | Pringles | c4ca4238a0b923820dcc509a6f75849b | | 16 | 18 | 20 | 1 | 20 | Eventyrbrus 0.5L | 6f4922f45568161a8cdf4ad2299f6d23 | | 17 | 16 | 20 | 1 | 20 | Coca-Cola 0.5L | c74d97b01eae257e44aa9d5bade97baf | | 18 | 15 | 30 | 1 | 30 | Coca-Cola 1.5L | 9bf31c7ff062936a96d3c8bd1f8f2ff3 | | 19 | 19 | 20 | 1 | 20 | Sprite 0.5L | 1f0e3dad99908345f7439f8ffabdffc4 | | 20 | 50 | 20 | 1 | 20 | Stratos bar | c0c7c76d30bd3dcaefc96f40275bdc0a | +---------+----+-------+-----+----------+------------------+----------------------------------+

paymentType

修改 SQL查询我到目前为止已经尝试过,存在更多,但这些是最新的。我认为这些是最正确的#34;。

+-----------+-------------+------------+----------------------------------+
| paymentID | paymentType | timestamp  |              rowid               |
+-----------+-------------+------------+----------------------------------+
|         3 | Kort        | 1424447799 | f457c545a9ded88f18ecee47145a72c0 |
|         4 | Kort        | 1424448791 | 1ff1de774005f8da13f42943881c655f |
|         5 | Kort        | 1424452822 | f457c545a9ded88f18ecee47145a72c0 |
|         6 | Kort        | 1424454483 | c4ca4238a0b923820dcc509a6f75849b |
|         7 | Kort        | 1424454665 | d9d4f495e875a2e075a1a4a6e1b9770f |
|         8 | Kontant     | 1424454799 | f457c545a9ded88f18ecee47145a72c0 |
|         9 | Kontant     | 1424454825 | f457c545a9ded88f18ecee47145a72c0 |
|        10 | Kort        | 1424454870 | 6ea9ab1baa0efb9e19094440c317e21b |
|        11 | Kontant     | 1424455510 | d9d4f495e875a2e075a1a4a6e1b9770f |
|        12 | Kort        | 1424455847 | c4ca4238a0b923820dcc509a6f75849b |
|        13 | Kontant     | 1424456025 | 6f4922f45568161a8cdf4ad2299f6d23 |
|        14 | Kontant     | 1424456099 | c74d97b01eae257e44aa9d5bade97baf |
|        15 | Kontant     | 1424456148 | 9bf31c7ff062936a96d3c8bd1f8f2ff3 |
|        16 | Kontant     | 1424456242 | c0c7c76d30bd3dcaefc96f40275bdc0a |
|        17 | Kort        | 1424456266 | c74d97b01eae257e44aa9d5bade97baf |
|        18 | Kort        | 1424456445 | c0c7c76d30bd3dcaefc96f40275bdc0a |
|        19 | Kort        | 1424456964 | 70efdf2ec9b086079795c442636b55fb |
|        20 | Kort        | 1424457701 | 1ff1de774005f8da13f42943881c655f |
+-----------+-------------+------------+----------------------------------+

预期的结果是将红牛Stor&#39;的数量列相加。 (即。)按小时分组的每一天。

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

select o.name
     , pt.rowid
     , sum(o.qty)
     , hour(from_unixtime(pt.timestamp))
     , day(from_unixtime(pt.timestamp))
from orders o
join paymentType pt using(rowid)
where o.name = 'Red Bull Stor'
group by o.name
       , o.rowid
       , hour(from_unixtime(pt.timestamp))
       , day(from_unixtime(pt.timestamp));

SQLFiddle