Example Analysis of thinkphp Common Query and Expression Query
- 2021-08-05 09:19:33
- OfStack
In this paper, thinkphp common query and expression query are described as examples. Share it for your reference. The specific analysis is as follows:
1. Common query method
a, string mode:
$arr=$m->where("sex=0 and username='gege'")->find();// String requires quotation marks
b, Array Mode:
$data['sex']=0;
$data['username']='gege';
$arr=$m->where($data)->find();// Pass on 1 Array, which defaults to and The relationship of (and)
Note: If you use the or relationship, you need to add array values
$data['sex']=0;
$data['username']='gege';
$data['_logic']='or';// Add to the array _logic Assign value to or (or) relationship
2. Expression query mode
$data['id']=array('lt',6);// The elements of the array are still arrays
$arr=$m->where($data)->select();
/*
EQ Equal to // Case doesn't matter
NEQ Not equal to
GT Greater than
EGT Greater than or equal to
LT Less than
ELT Less than or equal to
LIKE Fuzzy query */
$data['username']=array('like','%ge%');//like Wildcard query
$arr=$m->where($data)->select();// All containing ge All of them are found out
//NOTLIKE Does not contain
$data['username']=array('notlike','%ge%'); //notlike There is no space in the middle
$arr=$m->where($data)->select();
// Note: If 1 Fields should match multiple wildcards
$data['username']=array('like',array('%ge%','%2%','%5%'),'and');// If there is no number 3 Values and The default relationship is or Relationship
$arr=$m->where($data)->select();// Or (or) Can find out which 1 Just a value
//BETWEEN
$data['id']=array('between',array(5,7));
$arr=$m->where($data)->select();
//SELECT * FROM `tp_user` WHERE ( (`id` BETWEEN 5 AND 7 ) )
$data['id']=array('not between',array(5,7));// Attention, not And between Middle 1 There must be spaces
$arr=$m->where($data)->select();
//IN
$data['id']=array('in',array(4,6,7));
$arr=$m->where($data)->select();
//SELECT * FROM `tp_user` WHERE ( `id` IN (4,6,7) )
$data['id']=array('not in',array(4,6,7));
$arr=$m->where($data)->select();
//SELECT * FROM `tp_user` WHERE ( `id` NOT IN (4,6,7) )
I hope this article is helpful to everyone's thinkphp framework programming.