I'm querying on this table:
SKU
aaaa
bbbb
bbbb
NULL
Here's the query:
select *
from TEST as N
where N.SKU NOT IN (select SKU
from TEST
group by SKU
having count(*)>1);
I expect the query returns 'aaaa', however, it returns nothing.
The reason I expect that is because the subquery below only returns 'bbbb':
select SKU
from TEST
group by SKU
having count(*)>1
Therefore, 'aaaa' NOT IN the subquery result.
To show the bug please copy and paste these statements in your MySQL IDE to create schema:
drop table if exists TEST;
create table TEST(
SKU varchar(255)
);
insert into TEST values('aaaa'),('bbbb'),('bbbb'),(NULL);
Even more confusing, try this new table below, re-run the query, and check result:
drop table if exists TEST;
create table TEST(
SKU varchar(255)
);
insert into TEST values('aaaa'),('cccc'),('dddd'),('bbbb'),('bbbb'),(NULL);
Expect 'aaaa', 'cccc', 'dddd' are all returned, however 'aaaa' is missing, yet 'cccc' and 'dddd' are returned.
NULL. – ypercubeᵀᴹ Feb 27 '18 at 17:44TESTrestores the expected behavior. Also, error appears to be independent of the column type. I tried a similar test with INT instead of VARCHAR and got the same behavior. – Willem Renzema Feb 27 '18 at 19:06TESTtable does not, based on the schema provided. I tried adding another column as an auto increment primary key and there was no change in behavior. – Willem Renzema Feb 27 '18 at 19:10select sku from test group by sku having count(*)=1;. The bug - if there is one - is probably due to one (or a combination of) NOT IN, nulls, HAVING and correlated subqueries. For the real table, I'd try a NOT EXISTS query but that would require a UNIQUE column. That's why I asked for the PK. – ypercubeᵀᴹ Feb 27 '18 at 21:22select n.* from NS_DOWNLOAD as n where n.INACTIVE = 'No' and not exists (select * from NS_DOWNLOAD as b where b.INACTIVE = 'No' and b.sku = n.sku and b.internal_ID <> n.internal_ID);. The nulls should be in the result as well - even if they are a million! - (and index on(inactive, sku, internal_id)might be useful for efficiency.) – ypercubeᵀᴹ Feb 28 '18 at 01:04