0

達人!ハマった。カタログアイテムの価格は数量によって異なります。ここにテーブルの例があります:

items: just item definitions
-------------------------
item_id | item_title
-------------------------
1       | "sample item"
2       | "another item"

items_prices: prices dependent to item quantity. 
              Less price taken for more quantity of item
----------------------------
item_id | quantity  | price
----------------------------
1       | 1         | 100
1       | 5         | 80
1       | 10        | 60
2       | 1         | 120
2       | 3         | 100

cart
-------------------
item_id | quantity  
-------------------
1       | 20
2       | 2

単一のクエリで現在のカートのコストを取得することは可能ですか?

4

1 に答える 1

1
select sum(x.totalcost)
from (
    select c.item_id, c.quantity * ip.price as totalcost
    from cart c
    join items_prices ip
      on c.item_id = ip.item_id
    left join items_prices ip2
      on ip2.quantity > ip.quantity
      and c.quantity >= ip2.quantity
    where c.quantity >= ip.quantity
      and ip2.quantity is null
) x

items_priceに再度結合すると、基準をまだ満たしている数量が多い場合を除外できます。これは私たちが求めているものに近づいているはずです

于 2011-05-02T23:23:06.557 に答える