[英]How Can I Calculate remain days of the year from stored Date in mysql using php
[英]How can I calculate these date difference,multiplication in a MySQL stored procedure?
我試圖在mysql內實現一個過程,該過程基於計算返回天數。 輸入購買日期(added_date),然后完成計算。 應該相對簡單一些,但我似乎無法使其正常工作。 我不確定這是否適合“選擇語句”,如果這非常簡單,這是我第一次在程序中運行,我對此表示歉意。
如果有人可以告訴我我該怎么辦,將不勝感激。
謝謝!
我有一個稱為“ 計算 ”的表(表3),其中包含字段“ elap_yend”(int),在調用存儲過程之前,該字段當前為空。
表格1:
cid parent_cat category_name category_life
22 0 Office Equipment-M' 1080
23 0 Office Equipment-O' 1800
24 0 F & F' 3600
25 0 Staff assets', 1800
27 0 Motor vehicle', 2880
28 0 Air conditioner' 5400
29 0 Land & Building', 2160
30 0 Temporary Partition 365
31 0 Electrical Fittings 3600
32 0 Generator' 5400
33 0 Software' 1800
34 0 Computer-N' 2160
35 24 chair' 3600
Tble 2:
pid cid product_name product_price added_date cgst sgst igst total depre
60 22 RHFL\test\001 20000 2018-11-02 1800 1800 0 23600 o
61 27 RHFL\test\002 13500 2018-11-02 2345 2345 0 15930 12
62 29 RHFL\test\003 65000 2018-11-02 2345 2345 0 76700 12
63 31 RHFL\test\004 10000 2018-11-02 2345 2345 0 1180 12
64 24 RHFL\test\005 10000 2018-11-02 2345 2345 0 11800 1
65 24 RHFL\test\006 13500 2018-11-02 2345 2345 0 15930 12
66 34 RHFL\test\007 13500 2018-11-02 2345 2345 0 15930 12
67 22 RHFL\test\008 65004 2018-11-02 2345 2345 0 76704 12
68 25 RHFL\test\009 10000 0000-00-00 2345 2345 0 11800 12
69 22 RHFL\test\010 65000 0000-00-00 2345 2345 0 76700 10
70
表3:存儲過程的輸出應反映在下表中:
end_date elap_yend rem_days depre_cur cur_wdv depre_next next_wdv acc_depre
2018-11-02 0 0 0 0 0 0
輸出計算:
上面是零的示例輸出。 它不是確切的輸出,但是每當我調用存儲過程時,都應該在計算以下進行操作。
DELIMITER $$
CREATE PROCEDURE calculationTemp(
in till_yend date,
)
BEGIN
DECLARE till_yend date;
SELECT datediff(now(),added_date) INTO till_yend
FROM products
IF till_yend > 0 THEN
SET till_yend = select datediff(now(),added_date) from products;
ELSEIF till_yend < 0 THEN
SET till_yend = 0;
END IF;
insert into calculationTemp
END$$
計算方法:
截止年末:截止每年31 / March / xxxx(從產品表(No:2))到elapsed_yend = add_date。
這兩個日期之間的天數
missing_days = category_life-(addd_date(從產品(No:1))到日期的日期差)
category_life(來自類別表(編號:1))例如:移動設備的使用壽命為1080天。
當年折舊:depreciation_cur =(depre / category_life)* elapsed_yend
當年書面價值:
current_wdv =折舊-depreciation_cur
明年折舊:depreciation_next =(折舊/類別壽命)* DD =每年01 / April / xxxx到end_date之間的天差(來自計算表)
明年的書面價值:
next_wdv = current_wdv-折舊_下一個
累計折舊:
accumulate_depre =折舊_cur +折舊_next
在這里得到我的答案,以獲取有關改善我的商店程序的任何想法。 謝謝!!!
BEGIN
DECLARE cur_depre,cur_wtvalue,next_depre,next_wtvalue,accum_depre,pro_loss double(20,2);
DECLARE days,next_diff int;
CREATE TABLE IF NOT EXISTS calc AS (SELECT p.pid,p_date,days,next_diff,cur_depre,cur_wtvalue,next_depre,next_wtvalue,accum_depre,pro_loss,c.cid,p.added_date,c.category_life,p.depre,p.sale_status,p.sale_date,p.sale_amount from products p,products d,categories c WHERE p.pid=d.pid AND p.cid=c.cid
);
INSERT INTO calc (PID)
SELECT PID FROM products WHERE PID NOT IN (SELECT PID FROM calc);
UPDATE calc set days = datediff(p_date,added_date);
UPDATE calc set days = 0 WHERE datediff(p_date,added_date) < 0;
UPDATE calc set next_diff = datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) );
UPDATE calc set next_diff = datediff('2019-03-31',added_date) WHERE added_date>p_date;
UPDATE calc set cur_depre = (depre/category_life)*datediff(p_date,added_date);
UPDATE calc set cur_depre = 0 where (depre/category_life)*datediff(p_date,added_date)<0;
UPDATE calc set cur_wtvalue =(depre-(depre/category_life)*datediff(p_date,added_date));
UPDATE calc set cur_wtvalue = 0 WHERE added_date>p_date;
UPDATE calc set next_depre =( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) where added_date < p_date;
UPDATE calc set next_depre =( (depre/category_life) * datediff('2019-03-31',added_date)) WHERE added_Date > p_date;
UPDATE calc SET next_wtvalue = depre -(depre/category_life)*datediff(p_date,added_date) - (( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) );
UPDATE calc SET next_wtvalue = depre - (( (depre/category_life) * datediff('2019-03-31',added_date))) WHERE added_date>p_date;
UPDATE calc SET accum_depre = ((depre/category_life)*datediff(p_date,added_date))+( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) ;
UPDATE calc SET accum_depre =( (depre/category_life) * datediff('2019-03-31',added_date)) WHERE added_Date > p_date;
UPDATE calc SET pro_loss = sale_amount-(depre -(depre/category_life)*datediff(p_date,added_date) - (( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) ) );
UPDATE calc SET pro_loss = 0 WHERE sale_status=0;
SELECT * from calc;
END
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.