Scenario: I have a table with duplicate data. One of the columns of this table is ddate, if it is empty/null I want to select that row (and remove it). But for some reason, I cannot find the null rows with a direct query.
Issue: When I run the following query (1):
select
`ddate`,
count(1) as `nb`
from instrument_nt
group by `ddate`;
I get the number of rows where ddate is NULL and where it has other values. But when I run query (2):
select count(*) from instrument_nt where `ddate` = Null;
or
select * from instrument_nt where `ddate` = NULL;
My query result is either 0 or empty.
Question: What is the difference between those two queries (1 and 2)? How can I properly delete the data that has null/missing dates?
NULL mean unknow it's a value.
If you want to get NULL row you need to use IS NULL instead of eqaul NULL
select count(*) from instrument_nt where `ddate` IS Null;
What is the difference between those two queries (1 and 2)? How can I properly delete the data that has null/missing dates?
(1)
select count(*) from instrument_nt where `ddate` IS Null;
you will get the amount ddate is NULL from instrument_nt table.
(2)
select * from instrument_nt where `ddate` IS NULL;
you will get a result set which ddate is NULL;
Every null is defined to be different from every other null. Thus, equality to null is always false. See, for example, here, which describes this so-called "three value problem".
For this third class of value, you want to use IS, as in IS NULL or IS NOT NULL.
use the keyword IS NULL to check the null values in tables
For example:
select * from instrument_nt where `ddate` IS NULL;
MySQL null checks use the IS operator instead of =.
Your query should look like this: select * from instrument_nt whereddateIS NULL;
Related
I would like to see if in my table there exists a column where the entirety of its rows are null.
SELECT * FROM yourTableName
WHERE yourSpecificColumnName IS NULL
-> this will only return the values that are null but i wont know if yourSpecificColumnName is entirely null throughout the table
Using COUNT combined with HAVING:
COUNT
Returns either the number of non-NULL records for the specified columns, or the total number of records.
SELECT 'Entire_column_is_empty'
FROM yourTable
HAVING COUNT(yourSpecificColumnName) = 0;
or QUALIFY:
SELECT *
FROM yourTable
QUALIFY COUNT(yourSpecificColumnName) OVER() = 0;
Alternative approach:
SELECT 'Entire_column_is_empty'
FROM yourTable
HAVING MIN(yourSpecificColumnName) = MAX(yourSpecificColumnName);
select * from items where id is not null;
This query returns all correct records, but this one :
select * from items where id not in (null);
shows nothing. Can somebody explain why this happens?
null is not value, that is why we use is not null or is null expression.
in your second case
select * from items where id not in (null);
is equal to
select * from items where id != null;
which evaluates nothing because id is neither satisfies = null nor satisfies != null
Your second query will not work because null is not a value, it is undefined, that's why you can't use it with any sql operator. Instead you can re-write your second query like this :
select * from items where coalesce(id, '') not in ('');
But this will also exclude the result for id = '' So use of the first query is the best solution in this case which is :
select * from items where id is not null;
Get more information about NULL in mysql from Here
I have this kind of simple query that returns a not null integer field for a given id:
SELECT field1 FROM table WHERE id = 123 LIMIT 1;
The thing is if the id is not found, the resultset is empty. I need the query to always return a value, even if there is no result.
I have this thing working but I don't like it because it runs 2 times the same subquery:
SELECT IF(EXISTS(SELECT 1 FROM table WHERE id = 123) = 1, (SELECT field1 FROM table WHERE id = 123 LIMIT 1), 0);
It returns either field1 if the row exists, otherwise 0. Any way to improve that?
Thanks!
Edit following some comments and answers: yes it has to be in a single query statement and I can not use the count trick because I need to return only 1 value (FYI I run the query with the Java/Spring method SimpleJdbcTemplate.queryForLong()).
MySQL has a function to return a value if the result is null. You can use it on a whole query:
SELECT IFNULL( (SELECT field1 FROM table WHERE id = 123 LIMIT 1) ,'not found');
As you are looking for 1 record, (LIMIT 1) then this will work.
(SELECT field1 FROM table WHERE id = 123)
UNION
(SELECT 'default_value_if_no_record')
LIMIT 1;
Can be a handy way to display default values, or indicate no results found. I use it for reports.
See also http://blogs.uoregon.edu/developments/2011/03/31/add-a-header-row-to-mysql-query-results/ for a way to use this to create headers in reports.
You could include count(id). That will always return.
select count(field1), field1 from table where id = 123 limit 1;
http://sqlfiddle.com/#!2/64c76/4
You can use COALESCE
SELECT COALESCE(SUM(column),0)
FROM table
If someone is looking to use this to insert the result INTO a variable and then using it in a Stored Procedure; you can do it like this:
DECLARE date_created INT DEFAULT 1;
SELECT IFNULL((SELECT date FROM monthly_comission WHERE date = date_new_month LIMIT 1), 0)
INTO date_created
WHERE IFNULL((SELECT date FROM monthly_comission WHERE date = date_new_month LIMIT 1), 0) = 0;
With this you're storing in the variable 'date_created' 1 or 0 (if returned nothing).
Do search with LEFT OUTER JOIN. I don't know if MySQL allows inline VALUES in join clauses but you can have predefined table for this purposes.
k-a-f's answer works for selecting one column, if selecting multiple column, we can.
DECLARE a BIGINT DEFAULT 1;
DECLARE b BIGINT DEFAULT "name";
SELECT id, name from table into a,b;
Then we just need to check a,b for values.
if you want both always a return value but never a null value you can combine count with coalesce :
select count(field1), coalesce(field1,'any_other_default_value') from table;
that because count, will force mysql to always return a value (0 if there is no values to count) and coalesce will force mysql to always put a value that is not null
I ran three SQL queries in MySQL, but there is a logic problem.
select count(*) from keeper where code!=''; -- result1=2893193
select count(*) from keeper where code=''; -- result2=66
select count(*) from keeper; -- result3=3481069
I expected that result1 + result2 = result3, but in fact, result1 + result2 < result3. Why is this?
Using IS NOT NULL AND IS NULL in addition to ='' will make sure you get all rows that are both just empty like you are looking for already or have the column set as NULL
SELECT count(*) FROM keeper WHERE code!='' OR code IS NOT NULL;
SELECT count(*) FROM keeper WHERE code = '' OR code IS NULL
Three-valued logic attacks!
NULL and "" are two different things. NULL is considered to be neither equal nor not equal to "" and so neither of your queries will ever return it. I'd guess that the extra 500,000 records returned by your third query have code set to NULL. You can test for null fields using IS NULL or IS NOT NULL. If you do:
SELECT count(*) from keeper where code!='';
SELECT count(*) from keeper where code='';
SELECT count(*) from keeper where code IS NULL;
Those three results should add up to the total number of rows in your table.
1. select count(*) from keeper where code!=''
2. select count(*) from keeper where code=''
2.5. select count(*) from keeper where code is null
3. select count(*) from keeper
Note the one inserted before 3. NULL is considered a separate case from any other value, being neither equal to, nor not equal to, any other value (including another NULL).
Always use IS NuLL ans IS NOT NULL to get exact empty and non empty records respectively. it checks both empty and null values.
Try below:
select count(*) from keeper where code is NULL;
AND
select count(*) from keeper where code is NOT NULL
Alternatively you can use :
select count(*) from keeper where LENGTH(COALESCE(code ,'')) = 0
will give you all records with an 'empty' value for code , treating NULL as empty.
how to deal with NULL value in mysql where in CLAUSE
i try like
SELECT * FROM mytable WHERE field IN(1,2,3,NULL)
it not working
only work like :
SELECT * FROM mytable WHERE field IN(1,2,3) OR field IS NULL
how can i get it work in WHERE IN ? it is possible ?
There is a MySQL function called COALESCE. It returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
If you for example run SELECT COALESCE(NULL, NULL, -1); you will get -1 back because it's the first non-NULL value.
So the trick here is to wrap your expression in COALESCE, and add a value as the last parameter that you also add in your IN function.
SELECT * FROM mytable WHERE COALESCE(field,-1) IN (1,2,3,-1)
It will only match if field is 1,2 or 3, or if field is NULL.
As by my understanding you want to pull every record with 1,2,3 and null value.
I don't think its possible to put null in the IN operator. Its expects values and null is well.. not a value. So You really have to put the OR with the null to get the desired result.
Maybe this information from the MySQL Reference Manual helps:
To comply with the SQL standard, IN returns NULL not only if the expression on the left hand side is NULL, but also if no match is found in the list and one of the expressions in the list is NULL.
Using UNION as a subquery in IN operator can get tableIds as a list and from that can get results with the NULL value.
eg:
SELECT * FROM
mytable
WHERE mytable.id IN(
SELECT mytable.id
FROM mytable
where mytable.field IS NULL
UNION
SELECT mytable.id
FROM mytable
WHERE mytable.field IN(1,2,3)
)
Following statement should help:
SELECT * FROM mytable WHERE COALESCE(field,0) IN (1,2,3,0)