在使用连接时,我正在努力从qty
列中获得正确的和。当我试图从timestamp
表中获取paymentType
并将它们与rowid
表和orders
表中的paymentType
相连接,然后在timestamp
天对(day(from_unixtime(paymentType.timestamp)))
进行分组时,就会出现问题。
我想要使用qty
表中的timestamp
按小时计算paymentType
的总和,唯一的链接是rowid
(这是codeigniter的cart模块的rowid
)。逻辑上的问题(至少对我来说)是它在orders
表中存在的行比paymentType
表中的行多(因为这是每个产品的行数)(这只是为了跟踪是否使用了借方或现金)。当我将这些表连接在一起时,每小时的总和乘以orders.rowid <--> paymentType.rowid
中的每次命中。
如果解释不好,我很抱歉,但我希望这是可以理解的,我可以在这件事上得到帮助。
我已经尝试过至少10个查询,但似乎没有一个能像我想要的那样工作。
下面是我的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
表+-----------+-------------+------------+----------------------------------+
| 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 |
+-----------+-------------+------------+----------------------------------+
编辑:
到目前为止,我尝试过的sql查询还有很多,但这些是最新的。我认为这些是最“正确”的。
select orders.rowid, concat(convert(paymentType.timestamp,CHAR(11))) timestamp, orders.qty, orders.name
from orders
join paymentType
on orders.rowid = paymentType.rowid
order by paymentType.timestamp;
select orders.rowid, hour(from_unixtime(concat(convert(paymentType.timestamp,CHAR(11))))), orders.qty, orders.name
from orders
join paymentType
on orders.rowid = paymentType.rowid
#where orders.name = '".stripslashes($name)."'
order by paymentType.timestamp
;
select orders.qty, orders.name, orders.rowid, paymentType.rowid, paymentType.timestamp
from orders, paymentType
where orders.rowid = paymentType.rowid;
select qty, name, hour(from_unixtime(timestamp)) hour, day(from_unixtime(timestamp)) day
from orders_w_time
where name = 'Red Bull Stor'
;
select sum(qty) from orders
inner join (select distinct rowid from paymentType) pt
on orders.rowid = pt.rowid
where orders.name = 'Pølse';
select sum(orders.qty) totalqty, orders.name, pt.timestamp timestamp from orders ord
inner join (select timestamp from paymentType where paymentType.rowid = ord.rowid) pt
on orders.rowid = pt.rowid
where orders.name = 'Red Bull Stor';
select * from
(
select rowid, timestamp from paymentType
group by hour(from_unixtime(timestamp))
) pt
left join
(
select sum(qty), name, rowid from orders
) ord
on ord.rowid = pt.rowid
;
Select
paymentType.rowid,
orders.name,
orders.qty,
paymentType.timestamp
From
orders,
paymentType
Group By
day(from_unixtime(paymentType.timestamp));
select sum(orders.qty) Total
from orders
left join
(
select rowid,timestamp
from paymentType
) as paymet on orders.rowid = paymet.rowid
group by day(from_unixtime(paymet.timestamp))
;
select paymentType.rowid, ord.qty, timestamp
from paymentType
left join
(
select orders.rowid, qty
from orders
) as ord on ord.rowid = paymentType.rowid
;
预期的结果是将“红牛仓库”(即)的“数量”列按小时分组。
最佳答案
你可以试试这样的方法:
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
关于php - 尝试连接两个表时,总和不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29698954/