In your question, you are giving the indication that the difficulty is in the operation of inserting new rows in the database. After that, you have a query to collect results and give indication that the query is not working as intended, we will analyze:
- How to create the table so that things work as intended;
- Perform data insertion;
- Collect existing records.
Create the table
Since you want the columns named campo1
and campo2
to be null
by default, you have to give this indication in the table itself:
As you can see, we are indicating that the default value for column campo1
and campo2
is null
.
The code for creating the table would be:
CREATE TABLE IF NOT EXISTS 'tabela1' (
'id' int(13) NOT NULL AUTO_INCREMENT,
'campo1' int(13) DEFAULT NULL,
'campo2' date DEFAULT NULL,
PRIMARY KEY ('id')
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
Enter results
With the table properly prepared to accept null
in columns campo1
and campo2
, the insert query would be:
INSERT INTO 'tabela1' ( 'id' , 'campo1' , 'campo2' )
VALUES (NULL , NULL , NULL);
This will insert into the table a line type:
Conduct inquiries
Now that the table is in the desired form, queries looking for null
registrations can already be performed.
Query equal to the one you have in the question, that is, the column campo1
equal to 0
and column campo2
equal to null
or equal to 0000-00-00
:
SELECT *
FROM 'tabela1'
WHERE 'campo1' =0
AND (
'campo2' IS NULL
OR 'campo2' = '0000-00-00'
)
Which returns us exactly two records where both are with the value in the column campo1
equal to 0
and the value of column campo2
equal to NULL
in ID 1 and equal to 0000-00-00
in ID 3 :