mysql NULL value in where in CLAUSE - mysql

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)

Related

Selecting rows with null in integer columns using mysql query

I have a table named 'datatablecoulmn' with the following columns.
now i want all rows where the column FkID is NULL.FkID is an integer field
i tried the following queries
SELECT * FROM `datatablecoulmn` WHERE `FkID`=NULL
SELECT * FROM `datatablecoulmn` WHERE `FkID`<1
SELECT * FROM `datatablecoulmn` WHERE `FkID`='null'
All of these returns empty rows .Any help?
In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Any arithmetic comparison with NULL does not return true or false, but returns NULL instead., So, NULL != 'C' returns NULL, as opposed to returning true.
Use IS NULL condition in your query and try like this
SELECT * FROM `datatablecoulmn` WHERE `FkID` IS NULL
For handling NULL values MySQL provides three operators
IS NULL: operator returns true if column value is NULL.
IS NOT NULL: operator returns true if column value is not NULL.
<=>: operator compares values, which (unlike the = operator) is true
even for two NULL values.
You can refer to these links for more
Link 1,Link 2,Link 3
You can't compare with NULL. So you gotta check for YourColumn IS NULL (or maybe YourColumn IS NOT NULL.
NULL is a value like infinity is a number. In other words, not at all. NULL is the absence of certain information.
For the same reason that NaN (not a number) in IEEE754 floating point is not equal to other instances (or even the same instance) of NaN, nothing in SQL is equal to NULL, including NULL.
That's something that may sound strange but, when you think of the purpose of NULL, that of specifying unknown or inappropriate values, it makes sense.
In order to see if a value is NULL, you have to therefore use something like:
where COLUMN_NAME is null
More details on working with NULL in MySQL can be found here.
Use something like:
SELECT * FROM `datatablecoulmn` WHERE `FkID` is NULL
NULL is a placeholder to say there is the absence of a value. Which is why you can only use IS NULL/IS NOT NULL as predicates for such situations and not = or != or <> which is used by values.
Here is another way to exclude the records with FkID is NOT NULL:
SELECT D1.*
FROM datatablecoulmn D1
WHERE NOT EXISTS (SELECT D2.*
FROM datatablecoulmn D2
WHERE D2.`FkID` IS NOT NULL)

MySql, CONCAT and NOT IN, string values

I have such query but it doesn't select anything, but it should. So query
SELECT *
FROM _custom_access_call
WHERE CONCAT(type, name) NOT IN ('string1', 'string2', 'string3')
I manually add to table entry with null and '1sfgsg' values but it wasn't selected. Why? I need to select all entries that concat values is not in array. Help to deal with it.
If one of the values is NULL, then CONCAT() will return NULL. And NULL NOT IN (...) is always NULL. Also NULL IN (...) is always NULL. If you want to use NULL you should explicitally handle it. In this specific case, CONCAT_WS() helps, because it never returns NULL.
SELECT *
FROM _custom_access_call
WHERE CONCAT_WS('', type, name) NOT IN ('string1', 'string2', 'string3');
Also, note that this query cannot use any index.

Can I use NULL as part of a MySQL select statement where results are `IN (1,2,NULL)`?

Is it possible to include a NULL inside of a statement like SELECT * FROM my_table WHERE my_field IN (1,2,NULL);?
If not, how do you recommend allowing for only a few integers or NULL?
Is it best to use an OR like so:
SELECT *
FROM my_table
WHERE
my_field IN (1,2)
OR my_field IS NULL;
Use the OR, it's the clearest expression of what you want.
You can also provide a default value to use when my_field is NULL
SELECT *
FROM my_table
WHERE COALESCE(my_field, 1) IN (1,2);
But using a NULL in an IN-list will not work, since NULL is usually not equal to NULL. (It's not un-equal either — it's undefined).

How to return NULL when result is empty?

I have a simple query that selects one field and only one row, thus one value.
Is there any way to make it return NULL if the query results in an empty set? Instead of returning zero rows?
I think I need to use something with NOT EXISTS, THEN NULL but not certain about it.
select
(Your entire current Select statement goes here) as Alias
from
dual
dual is a built in table with a single row that can be used for purposes like this. In Oracle this is mandatory. MySQL supports it, but you can also just select a single value without specifying a table, like so:
select
(Your entire current Select statement goes here) as Alias
In either case you're selecting a single value. This means that:
If your select returns one value, that value is returned.
If your select statement returns one column, but no rows, NULL will be returned.
If your select statement returns multiple columns and/or multiple rows, this won't work and the query fails.
An easy way to do this is with aggregation:
select max(col)
from t
where <your condition here>
This always returns one row. If there is no match, it returns NULL.
Late reply but I think this is the easiest method:
SELECT
IFNULL((SELECT your query), NULL)
Use a UNION with a NOT EXISTS(original where clause)
select col1
from mytable
where <some condition>
union
select null
where not exists (
select * from mytable
where <some condition>)
You can use COALESCE for example:
SELECT COALESCE(Field1,NULL) AS Field1 FROM Table1
Edit 1:
sorry i mistake with return field as null not result set,for result set return as null use Union and Exist Function like this:
SELECT NULL AS Field1 FROM Table1 WHERE not EXISTS(SELECT Field1 FROM Table1 WHERE Field2>0)
UNION
SELECT Field1 FROM Table1 WHERE Field2>0

MySQL comparison with null value

I have a column called CODE in a MySQL table which can be NULL. Say I have some rows with CODE='C' which I want to ignore in my select result set. I can have either CODE=NULL or CODE!='C' in my result set.
The following query does not return a row with CODE as NULL:
SELECT * from TABLE where CODE!='C'
But this query works as expected and I know it is the right way to do it.
SELECT * from TABLE where CODE IS NULL OR CODE!='C'
My question is why does having only CODE!='C' does not return rows where CODE=NULL? Definitely 'C' is not NULL. We are comparing no value to a character here. Can someone throw some light as why it doesn't work that way?
In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Take a look at this MySQL Reference on NULL.
Any arithmetic comparison with NULL does not return true or false, but returns NULL instead., So, NULL != 'C' returns NULL, as opposed to returning true.
Any arithmetic comparison with 'NULL' will return false. To check this in SQL:
SELECT IF(NULL=123,'true','false')
To check NULL values we need to use IS NULL & IS NOT NULL operator.
Based on my tests and the documentation here: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
You can compare null and get a boolean result using <=>
NOTE: it looks like NOT EQ operator, but it's EQ operator
For example:
select x <=> y;
or
select #x <=> #y;
This also compares string vs null, string vs string, etc.
In SQL, the NULL value is a special value, not comparable with any other one.
The result of a direct comparison with a NULL is always NULL, although (unfortunately) you may find FALSE in some implementation.
To test a null value you should use IS NULL and IS NOT NULL.
SELECT *
FROM `table_name`
WHERE IFNULL(`column_name` != 'C', TRUE)
The specified problem can also appear in joins and the above answers aren't particularly helpful. The way I prefer to do it is by coalescing to otherwise impossible value. For example, this
select foo from bar
inner join baz on bar.x = baz.y
won't work if bar.x and baz.y are both nulls (join won't bring results). The workaround is to use e.g.
select foo from bar
inner join baz on coalesce(bar.x, -1) = coalesce(baz.y, -1)
where -1 is "impossible" value meaning it can never appear in the data set.
select * from user where application_id='1223333344' and name is null;
I use:
SELECT * from TABLE where NOT(CODE <=> 'C')