prepared mysql query where value may be NULL - mysql

I'm using node's mysql library and trying to do a query like so:
connection.query(`SELECT * FROM table WHERE name = ? AND field = ?`, ['a', value]);
The problem I'm running into is that sometimes value = 1 but sometimes value = null.
From my testing, results only return when the query is written as WHERE value IS null and doesn't work with WHERE value = null.
Q: How can I use the prepared query if the value may be null?

Sorry to make you disappointed, but YOU CANNOT
You should use different comparative statement, which is
WHERE value IS NULL
WHERE value = <your value>
Regards to the Mysql Reference, null values are treated differently, furthermore null values are a missing values. So You can't use arithmetic comparison for NULL
Here is the reference https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html

Nobody mentioned spaceship operator <=>, it works with null-to-null comparsions
Here is great spaceship operator description

Maybe you want try this:
SELECT * FROM table WHERE IFNULL(name, 'null') = 'null' AND IFNULL(field, 'null') = 'null'
But the next problem, you cannot fill your field with value 'null', or it will makes your query and data ambiguous.

Related

MySQL: Why can't I "column_name not in (null, 'foo')"?

I was writing a simple MySQL query along these lines today:
SELECT * FROM table_name WHERE column_name IS NOT NULL and column_name !='foo';
This returned the expected number of results. But I didn't love the syntax, and tried to make it more elegant:
SELECT * FROM table_name WHERE column_name NOT IN (NULL, 'foo');
Of course, that returned 0 results.
My question is this: Can you explain why a null value would not be in (NULL, 'bar')? I think it's because you can't compare NULL with NULL, at least philosophically. But why not?
Consider this:
# ruby
nil == nil
# => true
/* JavaScript */
undefined === undefined
// true
In those languages, and nil or undefined value is equal to any other nil or undefined value. Why not in SQL?
(Bonus points about close-to-the-metal implementation details of SQL, or philosophical differences in languages?)
In SQL, direct comparisons with NULL are neither true nor false, so the IN clause will not work. This is a fundamental feature1 of the ISO SQL standard.
See this Wikipedia entry:
Since Null is not a member of any data domain, it is not considered a "value", but rather a marker (or placeholder) indicating the absence of value. Because of this, comparisons with Null can never result in either True or False, but always in a third logical result, Unknown.
In this way, the concept of NULL in SQL is very different from Ruby's nil, or JavaScript's undefined. Ruby's nil is a 'value', so nil == nil is true. However, SQL's NULL is not a 'value', so NULL = NULL is unknown (but so is NULL <> NULL). For this reason, SQL provides a different operator for comparing NULL's—NULL IS NULL is true.
1: Some may disagree that this is in fact a feature.
The database structural query language SQL implements Three Valued Logic as a means of handling comparisons with NULL field content.
True
False
Unknown
The original intent of NULL in SQL was to represent missing data in a database, i.e. the assumption that an actual value exists, but that the value is not currently recorded in the database.
So comparison with UNKNOWN value gives indeterministic result which is evalauted to FALSE.
Can you explain why a null value would not be in (NULL, 'bar')?
Necause SQL NULLs are not equal to other NULLs. That's the way the SQL NULLs are defined, because the language uses three-value logic (3VL) *. In essence, NULL means "unknown", so all comparisons to it result in the unknown result - i.e. NULL. For example, the result of column_name = NULL is NULL, not false. This is the reason behind the introduction of IS NULL and IS NOT NULL operators into the SQL language.
Your first solution is correct. You could also use a less straightforward solution that converts NULLs to 'foo' before comparison, but the expression that results from this conversion requires more thinking to understand:
WHERE IFNULL(column_name, 'foo') != 'foo'
* The behavior of NULLs in SQL, especially the difference between the way they are treated during aggregation, has been a subject of a controversy. Several suggestions were made at how to "fix" this behavior, but none of them got widespread adoption because of their complexity.
In SQL, NULLs are not equal to other NULLs.
If I don't tell you my age, and you don't tell me your age, do we therefore have the same age?
No, we can't say that's a true predicate. We simply don't have enough information to say one way or the other. It's not exactly FALSE, but it's not definitely TRUE either.
Both column_name = NULL and column_name != NULL are UNKNOWN in SQL, and conditions are satisfied only of they are actually TRUE.
column_name IN (NULL, 'foo') is logically the same as (column_name = NULL) OR (column_name = 'foo').
Likewise, column_name NOT IN (NULL, 'foo') is logically the same as NOT ((column_name = NULL) OR (column_name = 'foo')), or (column_name != NULL) AND (column_name != 'foo').
Either way, the same rules about NULL comparisons apply.
It might be convenient if SQL were to automatically convert that to (column_name IS NULL) OR (column_name = 'foo') but that's not the way the language is standardly defined, for better or for worse.
IN Clause expects a value, a NULL is not a value. So You really have to put the OR with the null to get the desired result.
MySQL manual has a good read with this.
--EDIT--
To answer your comment let's compare NULL and a ZERO.
Zero is a value. It is the unique, known quantity of zero, which is used meaningfully in arithmetic/math. We can do things with zero.
Null on the other hand is a non-value, it's just a placeholder for a data value which is unknown or in other word not specified. Math can't be performed on NULL. Undefined is the other term of NULL. NULL doesn't exist so we can't do anything with it.
Null is not zero, null is not "" (empty string). Null is just a representation of an unknown piece of data.
I hope it's now clear. :)

COALESCE() to get Not null as well as NULL Values

I have my Mysql Query as :
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like ? and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE(?,'') = '' THEN RecordDateTimeUTC ELSE ? END)........
Now my query There are lott of fields(? --> this is where i am putting my data for filter perpose) upon which user can query so i am using coalesce but the disadvantage of using coalesce is it doesn't give me null values when i want to get all data present in employee table , so to get null values i have used isNull function.
Now i am testing the above query for two use cases
1-->User can pass percentage in RecordID:-In this case he should get all data either null as well as not null values :
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like '%'
and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE('','') = '' THEN RecordDateTimeUTC ELSE '' END)
2-->User can filter data based on RecordDateTimeUTC:- If user passes this parameter then he should get all not null data satisfying the above filteration.:-
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like '%' and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE('2012-07-09 11:11:00','') = '' THEN RecordDateTimeUTC ELSE '2012-07-09 11:11:00' END)
For the first use case it is working fine but for the second use case it is giving me filtered data as well as null Data
So what should be my query so that both my use cases are supported by the above single query.
I am using MYSQl Query Browser to test my query
Thanks in advance
It looks like you're trying to make parameters optional while using a single static SQL statement. In that case I would imagine you mean something like:
SELECT RecordID,ID,Name,Description,RecordDateTimeUTC,Location
FROM eemployee
WHERE (#RecordId IS NULL OR RecordID LIKE #RecordId)
AND (#RecordDateTimeUTC IS NULL OR RecordDateTimeUTC = #RecordDateTimeUTC)
If the parameter value is NULL, it will be omitted completely from the filter, otherwise the passed value will be used.
Note you can't use ? here because of the need to re-use the parameter - unless you want to specify each parameter twice, that is. Specifying a name makes the query a lot more readable IMHO.

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')

MySQL: selecting rows where a column is null

I'm having a problem where when I try to select the rows that have a NULL for a certain column, it returns an empty set. However, when I look at the table in phpMyAdmin, it says null for most of the rows.
My query looks something like this:
SELECT pid FROM planets WHERE userid = NULL
Empty set every time.
A lot of places said to make sure it's not stored as "NULL" or "null" instead of an actual value, and one said to try looking for just a space (userid = ' ') but none of these have worked. There was a suggestion to not use MyISAM and use innoDB because MyISAM has trouble storing null. I switched the table to innoDB but now I feel like the problem may be that it still isn't actually null because of the way it might convert it. I'd like to do this without having to recreate the table as innoDB or anything else, but if I have to, I can certainly try that.
SQL NULL's special, and you have to do WHERE field IS NULL, as NULL cannot be equal to anything,
including itself (ie: NULL = NULL is always false).
See Rule 3 https://en.wikipedia.org/wiki/Codd%27s_12_rules
SELECT pid FROM planets WHERE userid IS NULL
As all are given answers I want to add little more. I had also faced the same issue.
Why did your query fail? You have,
SELECT pid FROM planets WHERE userid = NULL;
This will not give you the expected result, because from mysql doc
In SQL, the NULL value is never true in comparison to any other value, even NULL. An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression.
Emphasis mine.
To search for column values that are NULL, you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression
Solution
SELECT pid FROM planets WHERE userid IS NULL;
To test for NULL, use the IS NULL and IS NOT NULL operators.
operator IS NULL tests whether a value is NULL.
operator IS NOT NULL tests whether a value is not NULL.
MySQL comparison operators
There's also a <=> operator:
SELECT pid FROM planets WHERE userid <=> NULL
Would work. The nice thing is that <=> can also be used with non-NULL values:
SELECT NULL <=> NULL yields 1.
SELECT 42 <=> 42 yields 1 as well.
See here: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to
Info from http://w3schools.com/sql/sql_null_values.asp:
1) NULL values represent missing unknown data.
2) By default, a table column can hold NULL values.
3) NULL values are treated differently from other values
4) It is not possible to compare NULL and 0; they are not equivalent.
5) It is not possible to test for NULL values with comparison
operators, such as =, <, or <>.
6) We will have to use the IS NULL and IS NOT NULL operators instead
So in case of your problem:
SELECT pid FROM planets WHERE userid IS NULL
Had the same issue where query:
SELECT * FROM 'column' WHERE 'column' IS NULL;
returned no values.
Seems to be an issue with MyISAM and the same query on the data in InnoDB returned expected results.
Went with:
SELECT * FROM 'column' WHERE 'column' = ' ';
Returned all expected results.
SELECT pid FROM planets WHERE userid is null;
I had the same issue when converting databases from Access to MySQL (using vb.net to communicate with the database).
I needed to assess if a field (field type varchar(1)) was null.
This statement worked for my scenario:
SELECT * FROM [table name] WHERE [field name] = ''

null used with logical operator

I have a table with name,age and address.I have totally five rows of data in the table.For some rows the age is left null.I have to display all the data where age is not null.
select * from sample_table where (age !=null);
But no output is displayed and it doesn't give an error also.Please explain this.Thanks.
With NULL you have to use IS or IS NOT. The following query should work:
SELECT * FROM sample_table WHERE (age IS NOT NULL)
The explanation from MySQL
The concept of the NULL value is a common source of confusion for
newcomers to SQL, who often think that
NULL is the same thing as an empty
string ''. This is not the case.
In SQL, the NULL value is never true in comparison to any other value,
even NULL. An expression that contains
NULL always produces a NULL value
unless otherwise indicated in the
documentation for the operators and
If you want to search for column values that are NULL, you cannot use
an expr = NULL test.
To look for NULL values, you must use the IS NULL test.
You have to use IS NULL or IS NOT NULL.
You can not compare directly against NULL because it is not value (no pedants please!)
NULL on Wikipedia
MySQL, Sybase, SQL Server... it's all the same