MYSQL return row - mysql

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.

Related

MySQL to filter result by using where & and condition

I'm trying to fetch some rows from a question table in MySQL like this:
SELECT *
FROM raise_question
where documentID = '1'
and delReq != 1 OR delReq IS NULL
However, some rows have different documentID. (Please see the picture). Is this a correct way (using HAVING instead of AND) to return the result I want ( which is it only contains specific documentID):
SELECT *
FROM raise_question
where documentID = '1'
having delReq != 1 OR delReq IS NULL
With AND query returns:
The expected result (only contains one documentID) after using Having I want is like this:
Your WHERE translate to
where (documentID = '1'
and delReq != 1) OR delReq IS NULL
, because AND has higher precedence than OR
Put bracket in your OR statement.
where documentID = '1'
and (delReq <> 1 OR delReq IS NULL)
Is this what you want?
select distinct documentid
from raise_question
where delReq <> 1 or delReq is null;
This returns document ids that have a non-1 delreq value.

Difference between (NULL) and empty in mysql column values

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)

Doing a select limiting character length doesn't return null values

I have a table full of address records and I'm trying to do a select with CHAR_LENGTH limiter for each column, but whenever I have a null value in a column it returns 0 results. Here's my example:
SELECT *
FROM SubjectDetailsView
WHERE ShipInFlag IS NULL
AND ZipCode IS NOT NULL
AND CHAR_LENGTH(ZipCode) < 11
AND CHAR_LENGTH(State) = 2
AND Address1 IS NOT NULL
AND CHAR_LENGTH(Address1) < 36
AND CHAR_LENGTH(Address2) < 36;
My main issue is with Address2. Zipcode, State, and Address1 always have a value, so that's never been an issue, but Address2 often has a null value. I get a result when I have data in the Address2 field, but if it's null, I get 0 results.
So is CHAR_LENGTH only looking for a string (1-35 in my case), and ignores null values? If so, how do I get my query to return results, if Address2 can have null values and not null values while still being less than 36 characters?
You have to test if address2 is null:
SELECT *
FROM SubjectDetailsView
WHERE ShipInFlag IS NULL
AND ZipCode IS NOT NULL
AND CHAR_LENGTH(ZipCode) < 11
AND CHAR_LENGTH(State) = 2
AND Address1 IS NOT NULL
AND CHAR_LENGTH(Address1) < 36
AND (Address2 IS NULL OR CHAR_LENGTH(Address2) < 36);
You can use IFNULL Function to make a string have length like empty :
SELECT * FROM SubjectDetailsView
WHERE ShipInFlag IS NULL
AND CHAR_LENGTH(IFNULL(ZipCode, 'empty')) < 11
AND CHAR_LENGTH(State) = 2
AND CHAR_LENGTH(IFNULL(Address1, 'empty')) < 36
AND CHAR_LENGTH(IFNULL(Address2, 'empty')) < 36;

Is there any diference between "!= NULL" and "IS NOT NULL" in MySQL?

Is there any difference between using "!= NULL" and using "IS NOT NULL"?
For example:
SELECT * FROM tbl_example WHERE a_field IS NOT NULL
and
SELECT * FROM tbl_example WHERE a_field != NULL
Yes, there is. != does not work properly with NULL1:
mysql> SELECT 1 != NULL, 1 IS NOT NULL;
+-----------+---------------+
| 1 != NULL | 1 IS NOT NULL |
+-----------+---------------+
| NULL | 1 |
+-----------+---------------+
1 row in set (0.00 sec)
BTW: != is not valid SQL, you should use the diamond operator <>.
1 In fact no comparison, except IS and IS NOT works.
You shouldn't compare values to null, becouse efect of this operation isn't true neither false - it's always unknown.
To check is value null, you should always use is null

MYSQL Using UPDATE, WHEN THEN with a NULL value

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