我有一张桌子pa_acc_status
,我附上了桌子的图像。
我想比较2个日期(一个是用户输入,另一个是列值),它将使用MYSQL在循环中从一列遍历到另一列。
例如:首先它将检查step_22_due_date
列是否为空并且step_22_due_date
< = USER_DATE
,如果这是真的,则退出该行并继续下一行否则对step_21_complete执行相同操作,然后执行step_21_due_date,然后执行step_1_complete
即step_22_due_date
- > step_21_complete
- > step_21_due_date
- > step_1_complete
我想以两种方式输出
案例1:适用于USER_DATE = 2016-09-03
首先喜欢这个
id | step_1_complete | step_21_due_date | step_21_complete | step_22_due_date | user_id
1 | NULL | 2016-03-12 | NULL | NULL | 1
2 | NULL | 2016-03-12 | NULL | NULL | 1
3 | NULL | NULL | NULL | 2016-04-15 | 2
4 | NULL | 2016-03-12 | NULL | NULL | 2
5 | NULL | NULL | 2016-03-12 | NULL | 2
第二个是日期的计数,它不是空的group by user_id
step_1_complete | step_21_due_date | step_21_complete | step_22_due_date | user_id
0 | 2 | 0 | 0 | 1
0 | 1 | 1 | 1 | 2
案例2:适用于USER_DATE = 2016-03-10
首先喜欢这个
id | step_1_complete | step_21_due_date | step_21_complete | step_22_due_date | user_id
1 | 2016-03-08 | NULL | NULL | NULL | 1
2 | 2016-03-08 | NULL | NULL | NULL | 1
3 | 2016-03-08 | NULL | NULL | NULL | 2
4 | 2016-01-03 | NULL | NULL | NULL | 2
5 | NULL | 2016-03-08 | NULL | NULL | 2
第二个是日期的计数,它不是空的group by user_id
step_1_complete | step_21_due_date | step_21_complete | step_22_due_date | user_id
2 | 0 | 0 | 0 | 1
2 | 1 | 0 | 0 | 2
我不知道是否可以使用MySQL,任何帮助或建议都会有所帮助。
答案 0 :(得分:0)
最后,我得到了解决方案,我的问题,
对于数据列表
SET @USER_DATE = '2016-03-08';
SELECT *
FROM
(SELECT `id`,
`user_id`,
`step_1_complete`,
NULL AS `step_21_due_date`,
NULL AS `step_21_complete`,
NULL AS `step_22_due_date`
FROM `pf_acc_status`
WHERE `step_1_complete` IS NOT NULL
AND `step_1_complete` <= @USER_DATE
AND (`step_21_due_date` IS NULL
OR step_21_due_date >= @USER_DATE)
UNION ALL SELECT `id`,
`user_id`,
NULL AS `step_1_complete`,
`step_21_due_date`,
NULL AS `step_21_complete`,
NULL AS `step_22_due_date`
FROM `pf_acc_status`
WHERE `step_21_due_date` IS NOT NULL
AND `step_21_due_date` <= @USER_DATE
AND (`step_21_complete` IS NULL
OR step_21_complete >= @USER_DATE)
UNION ALL SELECT `id`,
`user_id`,
NULL AS `step_1_complete`,
NULL AS `step_21_due_date`,
`step_21_complete`,
NULL AS `step_22_due_date`
FROM `pf_acc_status`
WHERE `step_21_complete` IS NOT NULL
AND `step_21_complete` <= @USER_DATE
AND (`step_22_due_date` IS NULL
OR step_22_due_date >= @USER_DATE)
UNION ALL SELECT `id`,
`user_id`,
NULL AS `step_1_complete`,
NULL AS `step_21_due_date`,
NULL AS `step_21_complete`,
`step_22_due_date`
FROM `pf_acc_status`
WHERE `step_22_due_date` IS NOT NULL
AND `step_22_due_date` <= @USER_DATE ) AS `a`
ORDER BY a.`id` ASC
适用于count
数据
SET @USER_DATE = '2016-03-08';
SELECT a.`user_id`,
count(a.`step_1_complete`) AS step_1_complete,
count(a.`step_21_due_date`) AS step_21_due_date,
count(a.`step_21_complete`) AS step_21_complete,
count(a.`step_22_due_date`) AS step_22_due_date
FROM
(SELECT `id`,
`user_id`,
`step_1_complete`,
NULL AS `step_21_due_date`,
NULL AS `step_21_complete`,
NULL AS `step_22_due_date`
FROM `pf_acc_status`
WHERE `step_1_complete` IS NOT NULL
AND `step_1_complete` <= @USER_DATE
AND (`step_21_due_date` IS NULL
OR step_21_due_date >= @USER_DATE)
UNION ALL SELECT `id`,
`user_id`,
NULL AS `step_1_complete`,
`step_21_due_date`,
NULL AS `step_21_complete`,
NULL AS `step_22_due_date`
FROM `pf_acc_status`
WHERE `step_21_due_date` IS NOT NULL
AND `step_21_due_date` <= @USER_DATE
AND (`step_21_complete` IS NULL
OR step_21_complete >= @USER_DATE)
UNION ALL SELECT `id`,
`user_id`,
NULL AS `step_1_complete`,
NULL AS `step_21_due_date`,
`step_21_complete`,
NULL AS `step_22_due_date`
FROM `pf_acc_status`
WHERE `step_21_complete` IS NOT NULL
AND `step_21_complete` <= @USER_DATE
AND (`step_22_due_date` IS NULL
OR step_22_due_date >= @USER_DATE)
UNION ALL SELECT `id`,
`user_id`,
NULL AS `step_1_complete`,
NULL AS `step_21_due_date`,
NULL AS `step_21_complete`,
`step_22_due_date`
FROM `pf_acc_status`
WHERE `step_22_due_date` IS NOT NULL
AND `step_22_due_date` <= @USER_DATE ) AS `a`
GROUP BY a.`user_id`
ORDER BY a.`id` ASC