我正在尝试应用递归逻辑。
我有以下数据tbl_appointment
table (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
如何满足以上要求呢?