Sign of different Query

Asked

Viewed 41,617 times

6

I have a problem presenting the Query.

I got the field alvaraValidade which only receives Data fields. When I put nothing in that field, the field date is guarded 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 to:

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

But it doesn’t work.

  • By the way this query has several conditions, you are sure that the problem is the AlvaraValidade ?

  • Post the table structure, it is much easier to help.

4 answers

9

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 at least some of the conditions are working. Then you can test separately from the field AlvaraNumero, and see if it works.

Once both are in order, then you test the desired operator, be and or or, as the desired result.

5

The right would be (has a IS there that is not necessary):

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

Or:

... AND AlvaraValidade <> '0000-00-00'

3

Your query is wrong in this passage:

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

The right thing would be:

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

-2

Solution:


Difference sign sql or mysql is <> for example:


AlvaraValidade <> '0000-00-00'

ie use <> in place of !=.

  • 8

    Both comparators are valid both != how much <>.

  • 2
  • 1

    @rodrigorigotti of a look here http://technet.microsoft.com/pt-br/Library/ms173258.aspx

  • 2

    @Silvioandorinha looks at the Mysql documentation, in "not Equal Operator": https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html. Another thing, his query is with the wrong syntax, I believe this is the problem.

  • 1

    look here @rodrigorigotti https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_not-Equal

  • 1

    Well, what matters is the standard. Although they are accepted both have had situations within Stored Procedures (Mysql) where the != had a strange behavior. Stick to reading: http://en.wikipedia.org/wiki/SQL#Operators

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.