Sign of different Query

6

I have a problem submitting Query.

I have the alvaraValidade field that only receives Date fields. When I do not put anything in this field, the date field is saved as 0000-00-00 .

$sql = "Select * From tb_trabalhador where AlvaraNumero is Not Null and AlvaraValidade is Not Null ...

And in this query I want to show those that are not NULL, that is, those that are NULL are not shown. And in this case the null field does not work because the field is 0000-00-00 .

I've tried:

(...) AlvaraValidade is != '0000-00-00'

But it does not work.

    
asked by anonymous 25.02.2014 / 13:38

4 answers

7

First, you have to understand that Null , zero, and 0000-00-00 are different things. Also, you have to consider whether you are using datetime or date.

For example, you could test the query just like this:

Select * From tb_trabalhador where AlvaraValidade != '0000-00-00 00:00:00'

or so:

Select * From tb_trabalhador where AlvaraValidade != '0000-00-00'

To make sure that at least one of the conditions is working. Then you can test the AlvaraNumero field separately and see if it works.

Once both are in order, then you test the desired operator, either and or or , depending on the desired result.

    
25.02.2014 / 14:22
2

Your query is wrong in this snippet:

... AND AlvaraValidade is != '0000-00-00'

The right thing would be:

... AND AlvaraValidade = '0000-00-00'
    
25.02.2014 / 13:42
1

The right one would be (there's an IS there that is not needed):

... AND AlvaraValidade != '0000-00-00'

Or:

... AND AlvaraValidade <> '0000-00-00'
    
25.02.2014 / 13:57
-2

Solution:

Difference sign sql or mysql is <> for example:

AlvaraValidade <> '0000-00-00'

or use <> instead of != .

    
25.02.2014 / 13:41