Home > Software design >  Hot to make a query which can have date or be empty
Hot to make a query which can have date or be empty

Time:03-04

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.

CodePudding user response:

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);

CodePudding user response:

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;
  •  Tags:  
  • sql
  • Related