I have following stored procedure in 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;
The stored procedure has 2 (IN) inputs, kid_Id
(integer) and received_Date
(date), and it works fine.
Question: What I want is if the received_Date
is NULL
then I want to view all dates, is that possible?
Say in other words:
the AND rewards.received_at = received_Date
should only work if I pass received_Date
otherwise return all dates.
Try this. Use OR
operator in where clause
to get all the rows if 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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With