thinkphp3.2.3 sql操作
資料庫操作
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 '_';
--------------------- 本文來自 信紙 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/bobby__star/article/details/70863562?utm_source=copy