iT邦幫忙

5

一個MySQL的點數使用應用問題

回答了這個問題.有興趣的,可以解一下看看.

https://www.facebook.com/groups/taiwanmysqlusergroup/posts/2065653506915717/

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
海綿寶寶
iT邦大神 1 級 ‧ 2022-01-26 08:22:27

不會純推

年輕人就是年輕人
如果是我才不會問這種半調子的問題

我會這麼問
如何以FIFO原則挑出所需筆數、扣點並顯示扣點前後點數
/images/emoticon/emoticon59.gif

https://ithelp.ithome.com.tw/upload/images/20220126/200017871pi8RAq3eo.png

0
breakgod
iT邦新手 2 級 ‧ 2022-01-27 09:39:23

建議是依日期排序,撈出資料
再搭配程式去作運算
檢查要到哪一筆資料才能足夠扣點

Select * FROM 資料表 ORDER BY 到期日

因為資料已經依到期日作排序
以迴圈方式將所有資料逐一檢查
先檢查2點夠不夠4點扣,結果不夠,還差2點,記錄下來
再檢查下一筆,3點夠不夠2點扣,結果足夠,迴圈結束(break)
所以就知道是這兩筆資料要拿來作扣點

1
rogeryao
iT邦超人 8 級 ‧ 2022-03-14 23:45:44
CREATE TABLE tab (
  stname char(10)
, point int
, exdate date 
);

insert into tab values
('店一', 5,  '2022/01/23'),
('店二', 3,  '2022/01/12'),
('店三', 2,  '2022/01/19'),
('店四', 1,  '2022/01/22');
select *
from (
select stname,exdate,point,
case when pointA < 0 then point 
when pointA > point then 0 
else point - pointA end `扣除點數`
from (
select *,
sum(point) over (order by exdate) - 4 as pointA
from tab
) as mm) as pp
where `扣除點數` > 0

Demo

我要發表回答

立即登入回答