I'm trying to figure out why I'm not getting a result from a MySQL Query I'm running.
I'm trying to replace a NULL value with a number with in a query, but I can't figure out what I'm doing wrong.
Here's my query:
UPDATE Details
SET HowHear_ID = CASE HowHear_ID
WHEN '' THEN 25
WHEN NULL THEN 25
WHEN 7 THEN 25
WHEN 8 THEN 5
WHEN 16 THEN 25
WHEN 17 THEN 16
END
WHERE HowHear_ID IN ('',NULL,7,8,16,17)
This Query will effect all but the NULL values.
What am I doing wrong??
No value will ever equal (or "unequal") NULL in SQL. Understand the following truth table:
NULL = NULL yields NULL -- not FALSE!
NULL != NULL yields NULL -- not TRUE!
[ANY] = NULL yields NULL -- not FALSE!
[ANY] != NULL yields NULL -- not TRUE!
Since the following are equivalent...
[expression] IN (a, b, c)
[expression] = ANY (a, b, c)
[expression] = a OR [expression] = b OR [expression] = c
... you cannot put NULL on the right hand side of an IN predicate. Interestingly, things get even worse when you put NULL on the right hand side of a NOT IN predicate:
[expression] NOT IN (a, b, c)
[expression] != ANY (a, b, c)
[expression] != a AND [expression] != b AND [expression] != c
If b were NULL, the whole expression will become NULL (or maybe FALSE), but never TRUE. This is also the case for NOT IN (subselect) predicates! So, never do this:
[expression] NOT IN (NULL, 1, 2)
The correct solution in your case uses a NULL predicate instead. Do this:
UPDATE Details
SET HowHear_ID = CASE
WHEN HowHear_ID = '' THEN 25
WHEN HowHear_ID IS NULL THEN 25 -- Use a NULL predicate here
WHEN HowHear_ID = 7 THEN 25
WHEN HowHear_ID = 8 THEN 5
WHEN HowHear_ID = 16 THEN 25
WHEN HowHear_ID = 17 THEN 16
END
WHERE HowHear_ID IN ('',7,8,16,17)
OR HowHear_ID IS NULL -- Use a NULL predicate here
Or this:
WHERE COALESCE(HowHear_ID, '') IN ('',7,8,16,17)
You can't reference NULL in a WHERE clause and get the results you expect. NULL behaves differently to other values.
If you need to reference it, you need to use the isnull() function.
in your case, you would write something like this:
WHERE HowHear_ID IN ('',7,8,16,17) or isnull(HowHear_ID)
By the way, you haven't specified the data type of the field. I assume it's an integer though. In that case, it might be better to check for zero rather than an empty string? (if it isn't an integer, then perhaps it should be)
You can achieve that with
WHERE HowHear_ID IN ('',7,8,16,17) OR HowHear_ID IS NULL
Related
I am working in a company where we use Spring -Hibernate and mysql database at backend.
There is a table tc_wallet
In this table i have an column tips_type which has values
Collection,Payable
'' (empty)
NULL --> No value has been initialized
Now when i fire a query:
SELECT *
FROM `tc_wallet`
WHERE
login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND `tips_type` != 'Collection'
I get results which has column value as '' (empty).
Login_id tips_type
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar
But even (NULL) is != 'Collection' should satisfy the above condition.
So according to me the result should have been .
Login_id tips_type
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar
gaurav.wakharkar (NULL)
gaurav.wakharkar (NULL)
Is there some issue while checking/comparing values with (NULL) ?
Does it behave differently ?
To check for nullness, you want to use IS NULL. Comparing NULL to something else with the equality operator (or the inequality operator) is always false.
Consider:
SELECT *
FROM `tc_wallet`
WHERE
login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND (`tips_type` IS NULL OR `tips_type` != 'Collection')
change your query to
SELECT
*
FROM
`tc_wallet`
WHERE login_id = 'gaurav.wakharkar'
AND `delete_flag` = 'F'
AND (`tips_type` != 'Collection' or `tips_type` is null)
I've a table with records in mysql as below and I would like to get the result, if no value in receivedfmuser and topm then no record (means 0 row) will be displayed, but mysql doesn't work as mysql still count it as 1 row?
select receivedfmuser, topm from tb_postatus where pono = 36 and receivedfmuser is not null and topm is not null
TABLE
|tid|pono|receivedfmuser|topm |
|1 |36 |02/02/2015 | |
|2 |27 |02/03/2015 |02/03/2015|
In your above example, the value in topm in row1 is actually an empty string value and not NULL. That is why it satisfies not null condition and is displaying the record. So, use the following
select receivedfmuser, topm from tb_postatus
where pono = 36 and
receivedfmuser != '' and
topm != ''
This wont display the rows having NULL and also those having an empty string value.
Try below query
select receivedfmuser, topm
from tb_postatus
where pono = 36
and receivedfmuser is not null
and receivedfmuser != ""
and topm is not null
and topm != ""
If in above example the value you are checking for null is topm, then the issue is your query because your query is validating "receivedfmuser" as not null value.
select t.receivedfmuser, t.topm, t.pono
from test t
where `receivedfmuser` IS NOT NULL;
receivedfmuser, topm, pono
'02/02/2015', NULL, '36'
'02/02/2015', '02/03/2015', '27'
Then changing the query to "topm"
select t.receivedfmuser, t.topm, t.pono
from test t
where `topm` IS NOT NULL;
receivedfmuser, topm, pono
'02/02/2015', '02/03/2015', '27'
The result show 1 row the one with topm not null.
With your example :
select t.receivedfmuser, t.topm, t.pono
from test t
where `topm` IS NOT NULL and t.pono = 36;
receivedfmuser, topm, pono
Not result is returned.
Hope this helps.
And yes, if the value you are checking to be null is actually an empty value, your query will not work as mysql will not understand null as an empty string.
I am trying to do a query that sees if fields are equivalent. However, whenever the field is NULL it returns a false result. I even tried doing the same thing with the column itself:
SELECT * FROM `mturk_completion` WHERE (`mturk_completion`.`imdb_url` =
`mturk_completion`.`imdb_url` AND `mturk_completion`.`worker_id` = 'A3NF84Q37D7F35' )
And it only returns results where the column is not NULL. Why is this so, and how do I get around it?
Your title is absolutely correct for any SQL implementation (not just MySQL). NULL is not equal to anything (including another NULL).
You need to use explicit IS NULL check or COALESCE() function (or its RDBMS-dependent alternatives) to set some default value in case of NULL.
Your comparison of mturk_completion.imdb_url to itself is redundant and should always return True, except when mturk_completion.imdb_urlis Null, in which case it will return Null.
That's because the operator = returns either True, False when comparisons can be made or Null, when either of the two operators is Null
Try this to illustrate the situation.
SELECT 1 = NULL; -- returns NULL
SELECT 1 != NULL; -- also return NULL
SELECT ISNULL(1 = NULL); -- returns 1
SELECT ISNULL(1 != NULL); -- returns 1
If you rewrite your query like below, your problems with ignoring NULLs will go away:
SELECT * FROM `mturk_completion` WHERE worker_id = 'A3NF84Q37D7F35'
I think you can use
(table.Field = table2.Field OR COALESCE(table.Field, table2.Field) IS NULL)
I'm getting data from database and using union for joining 2 table. But I need to use null || d.AM_Answer. Here I'm using only null, null. But it's taking only null value coming. If I stored the answer, I'm not getting answer. So, I need to use null || d.AM_Answer.
select
b.QM_ID, b.QM_QCM_ID, b.QM_Question, b.QM_Type, b.QM_Parent_Id, null, null
from
question_master b
INNER JOIN Assessment_master d
on ( d. AM_QM_ID = b.QM_Parent_Id
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
and d.AM_ASM_Local_Id = %# )
You can use ifnull to check if a field is null or not. If not null, then use field value.
Example:
IFNULL( d.AM_Answer, null )
This returns value of d.AM_Answer if it is not null.
And, you have some errors in your value comparison statements.
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
AND d.AM_ASM_Local_Id = %#
If %d and %# are place holders for numerics, then it is wrong way of input.
For runtime value inputs, you have to use ? place holder and use prepared statements to bind values on those parameters.
AND d.AM_HNM_ID = ?
AND d.AM_HM_ID = ?
AND d.AM_ASM_Local_Id = ?
Use your server side scripting language for value binding.
Refer to:
MySQL: IFNULL(expr1,expr2)
table person:
name
type enum('admin','user','random') NULL DEFAULT NULL
When this query is performed, it doesnt return the records that have a type of NULL
select * from person where type != 'admin';
null cannot be tested for with (in)equality statements. you need to use IS NULL. e.g.
select *
from person
where (type != 'admin') or (type IS NULL)
e.g. null is "contagious"
null > x -> null
null = x -> null
null = null -> null
null + 1 -> null
null * 1 -> null
etc... it's basically "unknown". Mixing known and unknown in sql makes the result unknown, always. Hence the special ifnull(), coalesce(), and "if null" tests/functions.
You can use the NULL safe equality operator in MySQL
select *
from person
where not type <=> 'admin'