Thinkphp field() 數據庫查詢函數使用


TP 查詢field小技巧

1. field 過濾掉某些字段

->where(['id'=>35])->field('delete_time',true)->find();

 

2. field 寫入字段合法性檢測

->field('title,email,content')->create();

 

3. field 操作新增字段

->field('id,name,price,0 as is_select')

 

4. field 運算 +-*/

->field('id,name,sales_origial_number+sales_actual_number as sales')

 

5. field 使用函數

->field('type,round(price) as price,time')
->field('type,count(id) as type_number')->group('type')->order('type_number')
->field(status,sum(price) as status_price)->group(status)->order(status_price)

 

6. field sum 統計次數

->field('count(id) as all_count,sum(star_rank = 1) as goods_review_count,sum(star_rank = 2) as middle_review_count,sum(star_rank = 3) as bad_review_count,sum(has_img = 2) as has_img_count')->where($where)->find();

 

7. field case when 的使用

->field('count(id) as all_count,IFNULL(SUM(CASE WHEN has_img = 2 THEN 1 END),0) as has_img_count,IFNULL(SUM(CASE WHEN star_rank = 1 THEN 1 END),0) as goods_review_count,IFNULL(SUM(CASE WHEN star_rank = 2 THEN 1 END),0) as middle_review_count,IFNULL(SUM(CASE WHEN star_rank = 3 THEN 1 END),0) as bad_review_count')->where($where)->find();

 

8. field 里作判斷

->field("if(substr(sn,1,2)='SY',1,0) as type")

 

復制代碼

9. field group 之后 某一個字段以“,”分割形式展示

->group(id)->field('group_concat(score order by `score ` desc) as score')復制代碼

 

10. group 之后 field 里面查詢最新一條里面的數據

->group('status')->field('SUBSTRING_INDEX(group_concat(id order by `id` desc),",",1) as id')復制代碼

 

文章轉載自 https://www.juchengvi.com/looknews/62


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM