MySQL中使用子查询时用in来限定外部查询的取值范围,当内部与外部查询相比较的字段值有一个为空时,结果集中就会被过滤掉,但是,在生成结果集的SQL语句后加上相关的查询条件时还是可以将要比较的字段值为空所在的那条记录正确完整的显示出来。
不理解……,请教(急)!谢谢……

解决方案 »

  1.   

    这段文字的上下文是什么?字面上看就是下面这个意思。 IN (....)中即使有 Null, 表记录中值为null的也不会被选出。但你可以再加一个条件 or c1 is null 来实现。mysql> select * from t1;
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |    1 |
    |  2 |    2 |
    |  3 |    3 |
    |  4 |    4 |
    |  5 |    5 |
    |  6 |    6 |
    |  7 |    7 |
    |  8 | NULL |
    |  9 | NULL |
    | 10 |   10 |
    +----+------+
    10 rows in set (0.00 sec)mysql> select *
        -> from t1
        -> where c1 in (1,4,5,7,10,null);
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |    1 |
    |  4 |    4 |
    |  5 |    5 |
    |  7 |    7 |
    | 10 |   10 |
    +----+------+
    5 rows in set (0.00 sec)
    mysql> select *
        -> from t1
        -> where c1 in (1,4,5,7,10,null)
        -> or c1 is null;
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |    1 |
    |  4 |    4 |
    |  5 |    5 |
    |  7 |    7 |
    |  8 | NULL |
    |  9 | NULL |
    | 10 |   10 |
    +----+------+
    7 rows in set (0.00 sec)mysql>
      

  2.   

    看了你另外一下贴子,和楼上的例子一样,你多了一个 or wage_infor.employee_Name=111 导致所有符合wage_infor.employee_Name=111 都会符合要求。SELECT wage_infor.employee_ID, 
    wage_infor.employee_Name,employee_infor.sex, wage_infor.Basic_Wage, 
    wage_infor.Allowance, wage_infor.OverTime_Wage, 
    wage_infor.Bounty , wage_infor.deduct, 
    wage_infor.Total_Wage, 
    wage_infor.beizhu 
    FROM wage_infor INNER JOIN employee_infor ON wage_infor.employee_ID = employee_infor.employee_ID  
    where Total_Wage in 
    (select (Basic_Wage+Allowance+OverTime_Wage+Bounty-deduct)  from wage_infor ) 
    and (wage_infor.employee_ID=1 or wage_infor.employee_Name=111)注意这对括号()
      

  3.   

    空与NULL是不同的,可以用IS NULL来判断
    and (wage_infor.employee_ID=1 or wage_infor.employee_Name=111)你的SQL语句可以优化