我试图计算从购买日期起每季度支付的利息
DATEDIFF
可以在不输入年份的情况下计算,并且可以连续计算两个日期之间每年的天数吗?
例如:DATEDIFF
当前日期没有年份,成熟日期没有年份
DATEDIFF ('10-25','*-01-01') while * is a wild card of years.
可以做到这一点还是有办法做到这一点?
当前代码:
// Escapes for security
$pdate = mysqli_real_escape_string($con, $_POST['pdate']);
$mdate = mysqli_real_escape_string($con, $_POST['mdate']);
$amt = mysqli_real_escape_string($con, $_POST['amt']);
$first = mysqli_real_escape_string($con, $_POST['first']);
$last = mysqli_real_escape_string($con, $_POST['last']);
$pid = mysqli_real_escape_string($con, $_POST['pid']);
$cno = mysqli_real_escape_string($con, $_POST['cno']);
$madd = mysqli_real_escape_string($con, $_POST['madd']);
$bene = mysqli_real_escape_string($con, $_POST['bene']);
$swc = mysqli_real_escape_string($con, $_POST['swc']);
$bacc = mysqli_real_escape_string($con, $_POST['bacc']);
$bank = mysqli_real_escape_string($con, $_POST['bank']);
$badd = mysqli_real_escape_string($con, $_POST['badd']);
$bno = mysqli_real_escape_string($con, $_POST['bno']);
// Query with DATEDIFF calculation
$sql="INSERT INTO contacts (
nodays, q1, q, pdate, mdate, amt, first, last,
pid, cno, madd, bene, swc, bacc, bank, badd, bno
)
VALUES (
DATEDIFF('$mdate','$pdate'),
(DATEDIFF('$mdate','$pdate') * '$amt' / 365 * 0.025),
'$pdate','$mdate','$amt','$first','$last',
'$pid','$cno','$madd','$bene','$swc','$bacc','$bank','$badd','$bno'
)";
// Executing and error checking of query
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Client record has been added to the database!";
// Close MySQL
mysqli_close($con);
?>
答案 0 :(得分:2)
仅供参考,这不是很有效(正如你在sql小提琴中看到的那样)。但这是一个开始。我会留下它,以防它帮助其他人和/或你获得有用的东西。
假设您购买的表格如 -
create table purchases (purchase_date date);
insert into purchases values ('2014-10-25');
创建一个名为generator的表,其中包含n行,n表示您将来想要查询的年数(在此示例中为5):
create table generator (n int);
insert into generator values (1),(2),(3),(4),(5);
然后,您可以运行以下内容,以便在购买日期之前的5天内获得该间隔的利息日期,并且每行的利息日期和购买日期之间的天数不同:
select purchase_date,
concat(@rw := @rw + 1, '-01-01') as interest_date,
datediff(purchase_date, concat(@rw := @rw + 1, '-01-01')) as date_diff
from purchases
cross join (select @rw := 2014) r
cross join generator
where n <= 5
union all
select purchase_date,
concat(@rx := @rx + 1, '-04-01') as interest_date,
datediff(purchase_date, concat(@rx := @rx + 1, '-04-01')) as date_diff
from purchases
cross join (select @rx := 2014) r
cross join generator
where n <= 5
union all
select purchase_date,
concat(@ry := @ry + 1, '-10-01') as interest_date,
datediff(purchase_date, concat(@ry := @ry + 1, '-10-01')) as date_diff
from purchases
cross join (select @ry := 2014) r
cross join generator
where n <= 5
order by 2