数据库操作
1、查询多条记录,返回:二维数组
$result = $model->select();
$result = $model->field()->select();
$result = $model->where()->select();
$result = $model->order()->select();
$result = $model->limit()->select();
$result = $model->field()->where()->order()->limit()->select();
2、删除记录,返回:受影响的行数
$result = $model->delete();
$result = $model->where()->delete();
3、添加记录,返回:主键值(主键自增长)、受影响的行数(主键不是自增长)
$result = $model->add(一维关联的数组);
$result = $model->data(一维关联的数组)->add();
4、查询一条记录,返回:一维关联数组
$result = $model->find();
$result = $model->field()->find();
$result = $model->where()->find();
5、修改记录,返回:受影响的行数
$result = $model->save(一维关联数组);
$result = $model->where()->save(一维关联数组);
6、执行select语句,返回:二维数组
$result = M()->query("select语句");
7、执行insert、update、delete语句,返回:受影响的行数
$result = M()->execute("insert|update|delete语句");
8、聚合查询,返回:数字
$result = $model->where()->count();
$result = $model->where()->sum(字段);
$result = $model->where()->avg(字段);
$result = $model->where()->max(字段);
$result = $model->where()->min(字段);
9、多表查询
$result = $model->join("多表查询")->select();
$result = $model->join("多表查询")->find();
$newsInfo = M("newsarticles")->join("newstypes on newsarticles.typeId=newstypes.typeId")->select();
10、修改一个指定的字段
$result = $model->where()->save(一维关联数组);
$result = $model->where()->setField("字段名","字段值");
11、查询一个指定的字段
$result = $model->where()->getField("字段名");
12、让字段值递增
$result = $model->where()->setInc("字段名");//让字段值加1
$result = $model->where()->setInc("字段名",3);//让字段值加3
13、让字段值递减
$result = $model->where()->setDec("字段名");//让字段值减1
$result = $model->where()->setDec("字段名",3);//让字段值减3
SELECT * FROM [user] WHERE username LIKE '%三%'
Sql代码
select * from user where username like '%huxiao';
select * from user where username like 'huxiao%';
select * from user where username like '%huxiao%';
另外,如果需要找出username中既有“三”又有“四”的记录,请使用and条件
SELECT * FROM [user] WHERE u_name LIKE ‘%三%’ AND u_name LIKE ‘%四%’
若使用 SELECT * FROM [user] WHERE u_name LIKE ‘%三%四%’
虽然能搜索出“三二四”,但不能搜索出符合条件的“张四三”。
_:表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:(可以代表一个中文字符)
Sql代码
select * from user where username like '_';