'Hot to make a query which can have date or be empty
Query bellow WHEN I set OR is giving me all values from table, when I set AND a.kraj IS NULL doesnt give me any.
How can I make query to display all records from table where a.kraj < CURRENT_DATE() but a.kraj can be empty as it is not required to be set. a.kraj can have date but can be empty as well
SELECT a.id, b.naziv, a.pocetak, a.kraj, a.status
FROM radni_nalozi a
LEFT JOIN artikli b ON a.idArtikal = b.ArtId
WHERE a.status = 1 AND CURRENT_DATE() >= a.pocetak AND a.kraj < CURRENT_DATE() OR a.kraj IS NULL;
I had no luck, I am getting in circle with this no idea what has to be changed.
Solution 1:[1]
You want the condition a.kraj < CURRENT_DATE() or a.kraj itself is NULL. Use appropriate parentheses:
SELECT a.id, b.naziv, a.pocetak, a.kraj, a.status
FROM radni_nalozi a
LEFT JOIN artikli b ON a.idArtikal = b.ArtId
WHERE a.status = 1 AND
CURRENT_DATE() >= a.pocetak AND (a.kraj < CURRENT_DATE() OR a.kraj IS NULL);
Solution 2:[2]
Simply indicate the logical intent with parenthesis:
WHERE (a.status = 1 AND CURRENT_DATE() >= a.pocetak AND a.kraj < CURRENT_DATE())
OR a.kraj IS NULL;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Biegeleisen |
| Solution 2 | Stu |
