mysql if not null, 0 or "" - mysql

SELECT *
FROM table WHERE id IN ('21')
AND (content_id IS NULL OR content_id = 0 OR content_id = '')
Is there a shorter way of writing this condition.
I have a int() column that could be either: NULL, 0 or EMPTY.

You can use IFNULL function in MySQL.
select ____
from tbl
where IFNULL(content_id, 0) = 0

I think the shorter way is this:
SELECT *
FROM table
WHERE id IN ('21')
AND COALESCE(content_id IN ('0', ''), 1)
content_id IN ('0', '') may assume these values:
True if content_id is either '0' or ''
Null if content_id IS Null
False otherwise.
If it's Null, COALESCE will return 1 here, which is equivalent to True.

You can try COALESCE:
SELECT * FROM table WHERE id IN ('21')
AND COALESCE(content_id,0) =0;

Related

Difference between <> and NOT in SQL [duplicate]

CREATE TABLE `mycompare` (
`name` varchar(100) default NULL,
`fname` varchar(100) default NULL,
`mname` varchar(100) default NULL,
`lname` varchar(100) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `mycompare` VALUES('amar', 'ajay', 'shankar', NULL);
INSERT INTO `mycompare` VALUES('akbar', 'bhai', 'aslam', 'akbar');
INSERT INTO `mycompare` VALUES('anthony', 'john', 'Jim', 'Ken');
_____
SELECT * FROM mycompare WHERE (name = fname OR name = mname OR name = lname)
akbar bhai aslam akbar
select * from mycompare where !(name = fname OR name = mname OR name = lname)
anthony john Jim Ken
In the second select above, I expect the "amar" record as well because that name does not match with First, second or last name.
Any comparison with NULL yields NULL. To overcome this, there are three operators you can use:
x IS NULL - determines whether left hand expression is NULL,
x IS NOT NULL - like above, but the opposite,
x <=> y - compares both operands for equality in a safe manner, i.e. NULL is seen as a normal value.
For your code, you might want to consider using the third option and go with the null safe comparison:
SELECT * FROM mycompare
WHERE NOT(name <=> fname OR name <=> mname OR name <=> lname)
You cannot use relational operators with NULL. The only operators that work with NULL are IS NULL and IS NOT NULL.
I had the same problem when I was writing update triggers and wanted to execute portion of code only when two values different. Using IS NULL and XOR came in handy:
SELECT 1!=1; -- 0
SELECT 1!=2; -- 1
SELECT 1!=NULL; -- NULL
SELECT NULL!=NULL; -- NULL
SELECT NULL IS NULL XOR 1 IS NULL; -- 1
SELECT NULL IS NULL XOR NULL IS NULL; -- 0
SELECT 1 IS NULL XOR 1 IS NULL; -- 0
So I've ended up using:
IF (OLD.col != NEW.col) OR ((OLD.col IS NULL) XOR ( NEW.col IS NULL)) THEN ...
NULL values are omitted automatically if you do a value comparison (because NULL isn't a value). Your where clause basically means: Compare the values of the mentioned fields, if they have a value—otherwise false.
You'd have to include a separate check for NULL if you want those rows, too.
You could probably get away with something like the following (assuming that mapping NULL to '' is not a problem):
SELECT * FROM mycompare
WHERE (ifnull(name,'') = ifnull(fname,'')
OR ifnull(name,'') = ifnull(mname,'')
OR ifnull(name,'') = ifnull(lname,''));
select * from mycompare
where !(ifnull(name,'') = ifnull(fname,'')
OR ifnull(name,'') = ifnull(mname,'')
OR ifnull(name,'') = ifnull(lname,''));

Mysql: CONCAT_WS('', col) vs (col is null OR col=0)

Is there any difference between:
CONCAT_WS('', column)=''
AND
column is null OR column=0 *(and optionally 'OR column="" ')*
Is one of them better/faster...?
SELECT my_fields FROM my_table
WHERE my_terms_clause='anything' AND CONCAT_WS( '', nb_check ) = ''
OR
SELECT my_fields FROM my_table
WHERE my_terms_clause='anything' AND (p.nb_check is null OR p.nb_check = 0)
I usually use "column is null OR column=0", but I just want "expert's tips".
You should definitely use:
where col is null or column = 0
First, the intention of the code is much clearer. Second, the function call prevents the optimizer from using an index. To be honest,the or also makes it hard for the optimizer to use an index.
Probably the most efficient way to write the query is using union all:
SELECT my_fields
FROM my_table p
WHERE my_terms_clause = 'anything' AND p.nb_check is null
UNION ALL
SELECT my_fields
FROM my_table p
WHERE my_terms_clause = 'anything' AND p.nb_check = 0;
This can take advantage of an index on my_table(my_terms_clause, nb_check).
If a column holds the value of 0, then concat_ws() with empty string as separator will return '0', not '', so the 2 expressions are not equal. If you need to check for null or 0, then better use that version, that actually checks this condition.

JPQL / SQL query: where like '%' include null values

I have a simple JPQL query. (But this applies also to an sql query..)
FROM DomainObj d where d.field1 like 'TEST%' and d.field2 like '%';
If the DB contains the following row:
1) field1 -> 'TEST'; field2 -> null
the query return nothing!
If the DB contains the following values:
2) filed1 -> 'TEST'; field2 -> ''
the query return the row!
How can I include also null values while searching for like '%' keeping the query as simple as possible (avoiding and/or clause?)
I'm implementing searching funcionality of an entity in the db.. and I also search by many fields at the same time..
Thank you
Marco
You can't directly use nulls in equality tests, because null is un-equal to everything, including itself. That's why there's the is null test, e.g:
select null = null -> null
select null <> null -> null
select 1 = null -> null
select 1 <> null -> null
select 1 + null -> null
essentially null is contagious, and will nullify anything it's compared to, or added in to.
So yes, you'll have to do
SELECT ... WHERE somefield LIKE '%...%' or somefield IS NULL
Try this:
...
and IFNULL(d.field2, '') like '%'
...
where ...
and (field2 = '' or field2 is null)
Note that the condition field2 like '%' is nonsensical because it matches any text except null. If you added or field2 is mull to it you would match everything, so logically you should just remove the condition on field2
Use IS NULL:
where d.field1 like 'TEST%' OR d.field2 IS NULL;
FROM DomainObj d where (d.field1 like 'TEST%' or d.field1 IS NULL) and (d.field2 like '%' or d.field2 IS NULL)

MYSQL Null issues

I've got a simple DB query. Basically I want to select all rows that are NOT equal to a certain set of strings, or is empty (if it's empty I do NOT want it to be selected).
This is what I've got:
select * from tbl_user where secretCode != ('S00' OR 'S05' OR 'A10' OR '')
The datatype of secretcode is CHAR(4), NULL (NO), DEFAULT NONE
What am I doing wrong here, should I be using NULL instead of '' ?
Thanks.
I think your query should be like this:
select * from tbl_user where secretCode NOT IN ('S00', 'S05', 'A10', '') AND
secretCode IS NOT NULL
select
*
from
tbl_user
where
secretCode not in ('S00', 'S05', 'A10') and secretCode not is null
Checking for NULL shoud be done with is null or not is null.
You should use the IS NOT NULL operator according to here
SELECT * FROM `tbl_users`
WHERE `secretCode` != ('S00' OR 'S05' OR 'A10') AND `secretCode` IS NOT NULL
use:
SELECT * FROM `tbl_users` WHERE `secretCode` <> ('S00' OR 'S05' OR 'A10') AND `secretCode` IS NOT NULL

Comparing with NULL values

CREATE TABLE `mycompare` (
`name` varchar(100) default NULL,
`fname` varchar(100) default NULL,
`mname` varchar(100) default NULL,
`lname` varchar(100) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `mycompare` VALUES('amar', 'ajay', 'shankar', NULL);
INSERT INTO `mycompare` VALUES('akbar', 'bhai', 'aslam', 'akbar');
INSERT INTO `mycompare` VALUES('anthony', 'john', 'Jim', 'Ken');
_____
SELECT * FROM mycompare WHERE (name = fname OR name = mname OR name = lname)
akbar bhai aslam akbar
select * from mycompare where !(name = fname OR name = mname OR name = lname)
anthony john Jim Ken
In the second select above, I expect the "amar" record as well because that name does not match with First, second or last name.
Any comparison with NULL yields NULL. To overcome this, there are three operators you can use:
x IS NULL - determines whether left hand expression is NULL,
x IS NOT NULL - like above, but the opposite,
x <=> y - compares both operands for equality in a safe manner, i.e. NULL is seen as a normal value.
For your code, you might want to consider using the third option and go with the null safe comparison:
SELECT * FROM mycompare
WHERE NOT(name <=> fname OR name <=> mname OR name <=> lname)
You cannot use relational operators with NULL. The only operators that work with NULL are IS NULL and IS NOT NULL.
I had the same problem when I was writing update triggers and wanted to execute portion of code only when two values different. Using IS NULL and XOR came in handy:
SELECT 1!=1; -- 0
SELECT 1!=2; -- 1
SELECT 1!=NULL; -- NULL
SELECT NULL!=NULL; -- NULL
SELECT NULL IS NULL XOR 1 IS NULL; -- 1
SELECT NULL IS NULL XOR NULL IS NULL; -- 0
SELECT 1 IS NULL XOR 1 IS NULL; -- 0
So I've ended up using:
IF (OLD.col != NEW.col) OR ((OLD.col IS NULL) XOR ( NEW.col IS NULL)) THEN ...
NULL values are omitted automatically if you do a value comparison (because NULL isn't a value). Your where clause basically means: Compare the values of the mentioned fields, if they have a value—otherwise false.
You'd have to include a separate check for NULL if you want those rows, too.
You could probably get away with something like the following (assuming that mapping NULL to '' is not a problem):
SELECT * FROM mycompare
WHERE (ifnull(name,'') = ifnull(fname,'')
OR ifnull(name,'') = ifnull(mname,'')
OR ifnull(name,'') = ifnull(lname,''));
select * from mycompare
where !(ifnull(name,'') = ifnull(fname,'')
OR ifnull(name,'') = ifnull(mname,'')
OR ifnull(name,'') = ifnull(lname,''));