使用連接時,我很難從qty
列中獲得正確的總和。當我嘗試從paymentType
表得到timestamp
和orders
和paymentType
表對rowid
加入這些,然後分組對天timestamp
出現該問題(day(from_unixtime(paymentType.timestamp)))
嘗試連接兩個表時總和不正確
我想要的qty
總和通過使用timestamp
小時paymentType
表,唯一的鏈接是rowid
(這是codeigniter的購物車模塊的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
;
預期的結果是對按小時分組的每個日期的'Red Bull Stor'(即。)的數量列求和。
這將是一個很大的問題,如果你提供的一對夫婦把你最接近你的答案查詢。 –
@Cᴏʀʏ那麼,這將是足夠的。一個很好的問題將提供適當的DDL和期望的結果集。 – Strawberry
@Strawberry:夠公平的。至少在某人的第二篇文章的正確軌道:) –