我在MySQL中有以下存储过程
SELECT *
FROM rewards
LEFT JOIN tasks
ON tasks.id = rewards.task_id
AND rewards.received_at = received_Date
WHERE tasks.kid_id = kid_Id
ORDER BY tasks.id ASC;
存储过程有2个(IN)输入,kid_Id
(整数)和received_Date
(日期),并且工作正常。
问题:我想要的是,如果received_Date
是NULL
,那么我想查看所有日期,这可能吗?
换句话说:
AND rewards.received_at = received_Date
仅在我通过received_Date
时才有效,否则返回所有日期。
答案 0 :(得分:2)
试试这个。如果OR
where clause
中的received_Date is null
运算符获取所有行
SELECT *
FROM rewards
LEFT JOIN tasks
ON tasks.id = rewards.task_id
WHERE tasks.kid_id = kid_Id
AND (rewards.received_at = received_Date or received_Date = 0)
ORDER BY tasks.id ASC;