I am able to replicate my issue with a very simple case..
Explanation
I have a very simple table my_table with one column column1.
create table my_table (column1 varchar(58));
I have few values for this column, NULL is also one of them.
insert into my_table (column1) values ('value1'), ('value1'), ('value2'), (null), ('value2');
Problem
When I try to query for group by column1 It is giving expected results by grouping all NULLs together. However if I add a where clause on column1 something like
select count(1) as value_count, column1 from my_table where column1 <> 'value1' group by column1;
It is ignoring both value1 and NULL where I was expecting to ignore only value1.
With this simple case I could get a workaround for this by adding an OR condition, But it is a real pain to add this condition all over in my original case.
Could someone can explain me better why this behavior and how can I fix this?
This is because any comparison with a NULL does not produce a true or false result, but instead produces a NULL result. Consequently, the condition column1 <> 'value1' evaluates as NULL where column1 is NULL, and so NULL values are not selected.
You can get around this by using a function such as coalesce to test column1 - like so:
select count(1) as value_count, column1
from my_table
where coalesce(column1,'') <> 'value1'
group by column1;
Null means "a value, but I don't know what it is".
So is column1 <> 'value1'? If column1 is null, then "Is a value, but I don't know what it is, unequal to 'value1'"?
Clearly the answer is "I don't know. I don't know what the value is".
The only rows included by a where clause are those which pass the where clause test. We don't know if this row passes the test, so it will not be included in the query.
You could try making use of the MySQL IFNULL in your predicate:
select count(1) as value_count, column1 from my_table where ifnull(column1,'nullvalue') <> 'value1' group by column1;
Related
I want to browse through all values of two columns in a table:
if the value in column 1 is not null, select it, otherwise select the value in column 2 instead.
then sort the final result in alphabetical ascending order, wherever column its values came from.
I tried the following query but it doesn't work and I'm not even sure it is supposed to do what I want to do.
SELECT *
FROM table
ORDER BY (CASE WHEN col1 IS NOT NULL THEN 1 ELSE 2 END ),
col1 DESC,
col2 DESC)
Besides the fact that it doesn't work (nothing outputted), it seems to sort the values of each column separately while I want to sort the final set of values retrieved, regardless of the column they are from.
Thank you for your help.
If you want to fix it with the CASE expression, it'd look like the following:
SELECT *,
CASE WHEN col1 IS NOT NULL
THEN col1
ELSE col2
END AS col
FROM table
ORDER BY col
Although a nice option is using the COALESCE function. It returns the first non-null value in the list of arguments.
SELECT *, COALESCE(col1, col2) AS col
FROM table
ORDER BY col
Let's say I have a table
with
Column1
A
A
A
B
B
C
C
C
Column2
CH
FH
FH
BW
CH
AW
Now: I want to have a select sth---> result 6 different combinations of column1 and column 2.
If i say select count(column2): I'll take 8.I don't want it.
If i say select(distinct column2): I'll take 5.I don't want it either.
The 6 different results I look for are: A-QW,A-CH,B-FH,C-BW,C-CH,C-AW
I'm looking for combinations.
Can you helpt me?
Use GROUP BY clause:
SELECT Column1,Column2 FROM tblname GROUP BY Column1,Column2
Grouping will do what you need. It will conveniently eliminate the duplicates for you.
select column1, column2
from <your_table>
group by column1, column2
Pretty sure this will do what you want. demo here
update based on comment
select count(*) from (
select column1, column2
from <your_table>
group by column1, column2
) q
updated demo
In some cases, we can use an expression which combines the two columns...
As an example:
SELECT COUNT(DISTINCT CONCAT(t.column1,'-',t.column2)) AS cnt_combinations
FROM mytable t
This only works in the special case, where we know that a dash character isn't included as a character in column1 or column2. Adding that delimiter character isn't strictly necessary either.
In the more general case, this wouldn't produce the "correct" results if the contents of the columns included values like.
column1 column2 CONCAT(column1,'-',column2)
------- ------- ---------------------------
A -CW A--CW
A- CW A--CW
Since the result of the expression is the same value, those won't be seen as distinct values.
The same thing happens with NULL values, because CONCAT() will return NULL whenever any of the argument values is NULL.
For example, using the string NULL to represent a NULL value
column1 column2 CONCAT(column1,'-',column2)
------- ------- ---------------------------
NULL CW NULL
A NULL NULL
I have a simple query that selects one field and only one row, thus one value.
Is there any way to make it return NULL if the query results in an empty set? Instead of returning zero rows?
I think I need to use something with NOT EXISTS, THEN NULL but not certain about it.
select
(Your entire current Select statement goes here) as Alias
from
dual
dual is a built in table with a single row that can be used for purposes like this. In Oracle this is mandatory. MySQL supports it, but you can also just select a single value without specifying a table, like so:
select
(Your entire current Select statement goes here) as Alias
In either case you're selecting a single value. This means that:
If your select returns one value, that value is returned.
If your select statement returns one column, but no rows, NULL will be returned.
If your select statement returns multiple columns and/or multiple rows, this won't work and the query fails.
An easy way to do this is with aggregation:
select max(col)
from t
where <your condition here>
This always returns one row. If there is no match, it returns NULL.
Late reply but I think this is the easiest method:
SELECT
IFNULL((SELECT your query), NULL)
Use a UNION with a NOT EXISTS(original where clause)
select col1
from mytable
where <some condition>
union
select null
where not exists (
select * from mytable
where <some condition>)
You can use COALESCE for example:
SELECT COALESCE(Field1,NULL) AS Field1 FROM Table1
Edit 1:
sorry i mistake with return field as null not result set,for result set return as null use Union and Exist Function like this:
SELECT NULL AS Field1 FROM Table1 WHERE not EXISTS(SELECT Field1 FROM Table1 WHERE Field2>0)
UNION
SELECT Field1 FROM Table1 WHERE Field2>0
5, Not sure what am I doing wrong, please help. As it seems in a query on multiple column comparison to find least value,
NULL continues to show up as a Resultant instead of least value
SELECT
IF(col1 IS NULL OR col2 IS NULL OR col3 IS NULL OR col4 IS NULL OR col5 IS NULL,
COALESCE(col1,col2,col3,col4,col5),
LEAST(col1,col2,col3,col4,col5)
) As Resultant
from db.tablename
Group by Id;
Alternatively tried CASE select without much success.
Thanks
if null is considered 0
select least(ifnull(col1,0),ifnull(col2,0),ifnull(col3,0),ifnull(col4,0),ifnull(col5,0))
as Resultant
from db.tablename
Group by Id;
if null is condered max
select least(ifnull(col1,~0>>1),ifnull(col2,~0>>1),ifnull(col3,~0>>1),ifnull(col4,~0>>1),ifnull(col5,~0>>1))
as Resultant
from db.tablename
Group by Id;
For people with the case such that there are only two values to compare, you can do this:
GREATEST(ifnull(a,b), ifnull(b,a))
how to deal with NULL value in mysql where in CLAUSE
i try like
SELECT * FROM mytable WHERE field IN(1,2,3,NULL)
it not working
only work like :
SELECT * FROM mytable WHERE field IN(1,2,3) OR field IS NULL
how can i get it work in WHERE IN ? it is possible ?
There is a MySQL function called COALESCE. It returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
If you for example run SELECT COALESCE(NULL, NULL, -1); you will get -1 back because it's the first non-NULL value.
So the trick here is to wrap your expression in COALESCE, and add a value as the last parameter that you also add in your IN function.
SELECT * FROM mytable WHERE COALESCE(field,-1) IN (1,2,3,-1)
It will only match if field is 1,2 or 3, or if field is NULL.
As by my understanding you want to pull every record with 1,2,3 and null value.
I don't think its possible to put null in the IN operator. Its expects values and null is well.. not a value. So You really have to put the OR with the null to get the desired result.
Maybe this information from the MySQL Reference Manual helps:
To comply with the SQL standard, IN returns NULL not only if the expression on the left hand side is NULL, but also if no match is found in the list and one of the expressions in the list is NULL.
Using UNION as a subquery in IN operator can get tableIds as a list and from that can get results with the NULL value.
eg:
SELECT * FROM
mytable
WHERE mytable.id IN(
SELECT mytable.id
FROM mytable
where mytable.field IS NULL
UNION
SELECT mytable.id
FROM mytable
WHERE mytable.field IN(1,2,3)
)
Following statement should help:
SELECT * FROM mytable WHERE COALESCE(field,0) IN (1,2,3,0)