NOT IN 在 MYSql 中不起作用并且不存在没有给出正确的结果

问题描述

  1. 尝试使用 not in() 函数获取没有 NULL 或 '' 字符串的记录但不起作用。请提出任何建议。

     select * 
     from table 
     where colm1 = 'xyz' 
       and colm2 not in (NULL,'') 
       and id = 268594; 
    
  2. 在第二种情况下,我有表 A 和 B。表 B 可以有多个记录映射到一个表 A 记录。我想在表 B 上执行 where 子句。如果记录可用,则需要检查值是否为空或空。如何在不执行 2 次选择查询的情况下执行:

表A:

a_id 姓名
1 "xyz"

表 B:

B_id 类型 价值 A_id
1 "x" "abd" 1
2 "y" "cdv" 1

所以想要选择表 A 的记录,如果记录在表 B 中不存在,类型为 "x and A_id" =1,如果存在,则仅在值为 NULL 或 '' 时选择表 A 记录。

我尝试了这些查询,但它们不起作用:

select * 
from A 
where not exists (select * 
                  from B 
                  where type = 'x' 
                    and value = null 
                     or value = '' 
                    and A_id = a_id); 

解决方法

 select * 
 from table 
 where colm1 = 'xyz' 
   and colm2 not in (NULL,'') 
   and id = 268594; 

Try using IS NOT NULL and <> '',例如:

 select * 
 from table 
 where colm1 = 'xyz' 
   and colm2 IS NOT NULL AND colm2<>''
   and id = 268594;