對於那些要實現模糊判斷的查詢,例如大於、等於、小於之類的SQL查詢,可以使用表達式查詢方式。
查詢表達式格式:$map['字段名'] = array('表達式','查詢條件');
PS:表達式不區分大小寫。
範例:
//EQ:等於(=)
$map['id'] = array('eq', 1); //where 為id=1
//NEQ:不等於()
$ map['id'] = array('neq', 1); //where 為id1
//GT:大於(>)
$map['id'] = array('gt', 1); //where 為id>1
//EGT:大於等於(>=)
$map['id'] = array('egt', 1); //where 為id>=1
//LT:小於($map['id'] = array('lt', 1); //where 為id//ELT:小於等於($map['id'] = array( 'elt', 1); //where 為id//[NOT]LIKE:模糊查詢
$map['user'] = array('like', '%小%'); //where為like %小%
//[NOT]LIKE:模糊查詢
$map['user'] = array('notlike', '%小%'); //where 為not like %小%
//[ NOT]LIKE:模糊查詢的陣列方式
$map['user'] = array('like', array('%小%', '%蠟%'), 'AND');
//產生的SQL
SELECT * FROM `think_user` WHERE ( (`user` LIKE '%小%' AND `user`
LIKE '%蠟%') )
//[NOT] BETWEEN:區間查詢
$map['id'] = array('between','1,3');
//where 為`id` BETWEEN '1' AND '2'
//同上等效
$map['id'] = array('between' ,array('1','3'));
//[NOT] BETWEEN:區間查詢
$map['id'] = array('not between','1,3');
//where為`id` NOT BETWEEN '1' AND '2'
//[NOT] IN:區間查詢
$map['id'] = array('in','1,2,4');
// where 為`id` IN ('1','2','4')
//[NOT] IN:區間查詢
$map['id'] = array('not in','1,2, 4');
//where 為`id` NOT IN ('1','2','4')
//EXP:自訂
$map['id'] = array('exp',' in (1,2,4)');
//where 為`id` NOT IN ('1','2','4')
PS:使用exp 自訂在第二個參數直接寫where 語句即可
//EXP:自訂增加OR 語句
$map['id'] = array('exp', '=1');
$map['user'] = array('exp', '= "蠟筆小新"');
$map['_logic'] = 'OR';
//WHERE 為( (`id` =1) ) OR ( (`user` ="蠟筆小新") )