MYSQL 查询重要函数

mysql 查询的重要函数

   今天在做前台的同事反映,列表能查到的数据,以列表中的数据为条件,去查询却不能显示数据!

  sql语句是这样:

                select * from location  where    concat(loc.AddressLine1,loc.AddressLine2) like concat(concat('%',#Address#),'%');

  意思是要查询Location表下面的 "AddressLine1"和"AddressLine2"字段的;(在location表中用“AddressLine1” 和"AddressLine2" 来存储Address 数据);

  但是由于习惯一般是将 Address存在 "AddressLine1"字段中,所以“AddressLine2”字段为空;

     concat 函数的简单用法 

   mysql CONCAT(str1,str2,…)                        
返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。

如上所述,如果有任何一个参数为NULL,那么返回的值就是 NULL;

所以   select * from location  where    concat(loc.AddressLine1,loc.AddressLine2) like concat(concat('%',#Address#),'%'); 

每次返回的都是Null;

解决之道是:

  select * from location  where    concat(IFNULL(loc.AddressLine1,''),IFNULL('loc.AddressLine2','')) like concat(concat('%',#Address#),'%'); 

     IFNULL(expr1,expr2) 函数的简单用法 
如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值;

  以上的情况就是当loc.Address2,为空的时候,就返回  ' '

这样就避免查询没有结果的问题;

 

posted @ 2016-04-18 16:41  迭浪三千  阅读(305)  评论(0)    收藏  举报