gpt4 book ai didi

php - 每月注册和有日期的注册

转载 作者:行者123 更新时间:2023-11-29 02:36:02 25 4
gpt4 key购买 nike

我有一个 MySQL 表,其中包含每月和特定日期的固定价格注册,如下所示:

id       int(10)
date date
userid int(10)
montly tinyint(1)
price decimal(10,2)

有些数据可能是

id |    date    | userid | monthly | price
1 | 01/01 2011 | 1 | 1 | 200
2 | 01/02 2011 | 2 | 1 | 300
3 | 14/01 2011 | 1 | 0 | 400
4 | 15/02 2011 | 3 | 0 | 100
5 | 23/02 2011 | 2 | 0 | 600
6 | 05/03 2011 | 2 | 0 | 700

每月注册总是在当月的 1 号开始如果 monthly 为 1,则为每月注册,否则仅发生一次。

一个查询可以是(只是一个例子)

SELECT * 
FROM `fixedpriceregistrations`
WHERE `date` BETWEEN '01/02 2011' AND '02/04 2011'

除了这个输出:

id |    date    | userid | monthly | price
1 | 01/01 2011 | 1 | 1 | 200
2 | 01/02 2011 | 2 | 1 | 300
4 | 15/02 2011 | 3 | 0 | 100
5 | 23/02 2011 | 2 | 0 | 600
1 | 01/01 2011 | 1 | 1 | 200
2 | 01/02 2011 | 2 | 1 | 300
6 | 05/03 2011 | 2 | 0 | 700
1 | 01/01 2011 | 1 | 1 | 200
2 | 01/02 2011 | 2 | 1 | 300

我可以在 MySQL 中执行此操作吗?或者在 PHP 中如何执行此操作的最佳方式是什么?

在 PHP 中,我考虑循环遍历带有日期的注册并将它们添加到新数组中,当月份更改时,将添加每月注册。

最佳答案

用 php 来做。我会:

从表中获取您需要的所有记录(即自开始日期起)到一个数组中,$results

$output = Array();
foreach ($results as $row)
{
$output[] = $row;
if ($row['monthly'])
while ( ($row['date'] = add_a_month_to( $row['date'] )) < $end_date )
$output[] = $row;
}

然后对生成的 $output 数组进行排序。我将排序和 add_a_month_to 函数留给您。显然,它假定您使用的是合理的(可排序的)日期格式,例如2011-01-13

关于php - 每月注册和有日期的注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4995814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com