3

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

  1. Collection,Payable
  2. '' (empty)
  3. 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 ?

3

2 Answers 2

3

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')
Sign up to request clarification or add additional context in comments.

1 Comment

You should mention three-valued logic here to make the answer complete. Anyway, +1 :)
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.