我正在尝试应用递归逻辑.
我在tbl_appointment表中有以下数据(recur_type:1 =每周,2 =每月,0 =不递归):
appointment_id user_id date recur_type .....
18 56 2014-06-02 1
19 56 2014-06-15 2
20 56 2014-06-20 0
21 56 2014-07-20 2
我有以下标准来获取数据:如果我获取2014年7月(第7个月)的数据,那么
>获取了appointment_id(21)(1行)并且……
> appointment_id(18)是每周递归,7月重复被提取(4行):
appointment_id user_id date recur_type .....
18 56 2014-07-07 1
18 56 2014-07-14 1
18 56 2014-07-21 1
18 56 2014-07-28 1
注意:日期已更改,因为每周的约会是递归的,这意味着我会在每个日期添加7天. 2014-06-02 7天= 2014-06-09等.因此,7月,日期是2014-07-07.
> appointment_id(19)是每月递归,7月重复获取(1行):
appointment_id user_id date recur_type .....
19 56 2014-07-15 2
注意:日期已更改,因为每个月的约会是递归的,这意味着我会在该日期添加一个月.
最终输出是(总共6行):
appointment_id user_id date recur_type .....
21 56 2014-07-20 2
18 56 2014-07-04 1
18 56 2014-07-11 1
18 56 2014-07-18 1
18 56 2014-07-15 1
19 56 2014-07-15 2
我尝试了以下代码:
SELECT
tu.email,
ta.appointment_id,
ta.user_id,
ta.date,
ta.time,
ta.recur_type,
0 recursive
FROM
tbl_appointment ta
LEFT JOIN
tbl_user tu ON ta.user_id = tu.user_id
WHERE
1 AND YEAR(ta.date) = '2014'
AND MONTH(ta.date) = '06'
AND ta.user_id = 56
UNION ALL SELECT
tu.email,
ta.appointment_id,
ta.user_id,
ta.date,
ta.time,
ta.recur_type,
1 recursive
FROM
tbl_appointment ta
LEFT JOIN
tbl_user tu ON ta.user_id = tu.user_id
WHERE
1 AND recur_type = '2'
AND ta.user_id = 56
UNION ALL SELECT
tu.email,
ta.appointment_id,
ta.user_id,
ta.date,
ta.time,
ta.recur_type,
2 recursive
FROM
tbl_appointment ta
LEFT JOIN
tbl_user tu ON ta.user_id = tu.user_id
WHERE
1 AND recur_type = '1'
AND ta.user_id = 56
ORDER BY date DESC, time
如何满足上述要求?