87 thinkphp 和sql查询条件为某字段不为空的情况

1.今天遇到了一个问题,查询条件为某个字段不为空,于是构造map

$map['top']             =    1;
$map['sectorid']        =    1;
$map['thumbnail']	= array('NEQ',NULL);
一查,结果是空

生成的sql为

 
  
SELECT * FROM `nj_article` WHERE ( `top` = 1 ) AND ( `sectorid` = 1 ) AND ( `thumbnail` <> NULL ) LIMIT 1 

正确的sql应该是这个

 $topNews  =  $model ->query("SELECT * FROM `nj_article` WHERE ( `top` = 1 ) AND ( `sectorid` = 1 ) AND (`thumbnail`!='') ORDER BY publishtime desc LIMIT 1 ");

//sql写生成的语句
SELECT * FROM `nj_article` WHERE ( `top` = 1 ) AND ( `sectorid` = 1 ) AND (`thumbnail`!='') ORDER BY publishtime desc LIMIT 1 
//原因:使用=,~=,^=,<>与NULL比较的结果都是FALSE,所以在与NULL比较的时候,应该使用IS NOT NULL 或者IS NULL。
//思路是对的,写出来的SQL也不会报错,但是查询结果永远返回FALSE值

你可能感兴趣的:(mysql,php)