I have a column that has null values as well as other values such as 'deactivated'. I am trying to build a query that says "WHERE field <> 'deactivated" but it returns an empty result set. From my research it seems to be because it can't compare to the null values. But I haven't been able to figure out how to get around it.
Thanks
As it seems that you want nulls included in the result set, the correct condition would be
WHERE field <> 'deactivated' OR field IS NULL
Try looking for NULL specifically:
WHERE field <> 'deactivated' OR field IS NULL
FYI, you must use IS NOT and not a comparision operator because NULL doesn't equal anything. Even another NULL;
Since you're using MySQL, you can use the "equal to" operator:
WHERE NOT(field <=> 'deactivated')
In other, more SQL-standards compliant databases, you'd write
WHERE field IS DISTINCT FROM 'deactivated'
I have recently written a blog post on the DISTINCT predicate and how it is supported in various databases.
You can use MySQL's "null-safe equal" operator <=>:
WHERE NOT field <=> 'deactivated'
Related
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.
I have 2 tables with "like" fields and am simply trying to "flag" all records where the "like" fields are different. For example, I have a description field in table #1 and a description field in table #2.
I created a new field titled Description_Diff with the formula Descr_diff: IIf([Tbl 1 items].[description]<>[Tbl 2 items].[description],"diff","").
The issue I am having is that if one of the two values is Null/Blank it does not return a "diff". I apologize in advance if this answer had already been covered, but upon searching the site was unable to find the answer.
Try with Nz:
Descr_diff: IIf(Nz([Tbl 1 items].[description])<>Nz([Tbl 2 items].[description]),"diff",Null)
The real answer is that you can't compare Null using any logical operator.
So If 1 <> Null doesn't work as a comparison.
Have a read here http://allenbrowne.com/casu-11.html and http://allenbrowne.com/casu-12.html for a fuller explanation.
use coalesce
usage:
coalesce(value, defaultValue)
when comparing if it's null, throw in "" or -1 as a default value, then comparing with blank or default would return false
Use the coalesce in your query in the select
Select coalesce(id,-1), coalesce(firstname,"") From myTable
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] = ''
Can anyone help me understand or post any ideas concerning this where clause?
sql was here
I've changed the table name, but other than that, any idea what the developer was trying to do here?
There is nothing else after that, that's the where clause.
If (table.date_field = (select max(table2.exit_date) from table as table2)) is null the it'll return 1=1, which basically means there's no where clause at all.
Now let's look into that nasty expression. I can only assume that if "a = b" is not true then that's also equivalent to null, otherwise it seems like the first branch would always happen. It looks like it's trying to say "if the latest exit date is equal to the date field, select those, otherwise have no where clause". However, I don't think that this will work at all. It really looks like either way, each row will be selected.
The MySQL ifnull function returns the first argument if it is not null, otherwise the second argument. This looks like it tries to compare table.date_field to the max(table2.exit_date), and return true if the comarison was not possible due to nulls.
It looks to me like he is trying to find the row where table.date_field is equal to the maximum of table.exit_data. There is a check for null which I think would happen in any of these cases:
table is empty
all rows in table have exit_data set to NULL
table.date_field is NULL for the row in question
In any of these three cases, the row will be returned. I don't understand why he uses the string '1=1' instead of, to give some examples: 1=1, 1 or true, but it appears to work fine. In the first case I assume that there will be no rows in the result set anyway (depending on the rest of the query) so he was probably trying to handle one of the other two cases - I'd guess the last one.
This is only an explanation of what is happening. To understand why he is doing this, it would help if you gave a little more context.
MySQL is nonstandard in that true is really equal to the numeric value 1. Any expression that evaluates to true, or any nonzero value, satisfies the condition.
mysql> CREATE TABLE foo AS SELECT 1=1 AS f;
mysql> SHOW CREATE TABLE foo;
CREATE TABLE `foo` (
`f` INT NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
So the following WHERE clause is legal in MySQL, but not in most other SQL databases:
... WHERE 1;
Some people use 1=1 as a placeholder condition meaning true, but putting it in a string is meaningless because SQL expressions have no equivalent to an eval() function as other languages have. In this case, the leading character 1 in the string is implicitly cast to a numeric value 1, which is interpreted as true in MySQL. So it probably works as intended, but kind of by accident.
The use of IFNULL() is so that if either date_field or MAX(exit_date) is NULL, it returns the row. If you didn't use this function, then anything = NULL would evaluate as unknown, which means the row would not be returned.
It says basically if table.date_field = max exit date or if max exit_date is null or table.date_field is null return true. Will return false if max exit_date is not null and table.date_field is not null but they do not equal.
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