日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

yii2 mysql between_yii2:多条件多where条件下碰到between时,between语句如何处理呢?

發(fā)布時間:2024/10/8 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 yii2 mysql between_yii2:多条件多where条件下碰到between时,between语句如何处理呢? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

yii2:多條件多where條件下碰到between時,between語句如何處理呢?

我有一張表:

id,name,telphone,ticket_no,status,create_time等字段,

在出具多條件查詢時(當(dāng)不涉及到時間范圍或其他范圍),可以用如下語句:

if (!empty($params[‘id‘])) {

$where_condition[‘oid‘] = $params[‘id‘];

}

if (!empty($params[‘post_name‘])) {

$where_condition[‘post_name‘] = $params[‘post_name‘];

}

if (!empty($params[‘telephone‘])) {

$where_condition[‘telephone‘] = $params[‘telephone‘];

}

if (!empty($params[‘ticket_no‘])) {

$where_condition[‘ticket_no‘] = $params[‘ticket_no‘];

}

if ($params[‘status‘] != -1) {

$where_condition[‘status‘] = $params[‘status‘];

}

$where_condition[‘delete_flg‘] = 0;

$count = static::find()

->where($where_condition)

->andFilterWhere([‘between‘,‘CREATE_TIME‘,$start_date, $end_date])

->count();

當(dāng)涉及到create_time時間范圍查詢時,那么問題來了,between怎么加進去?$where_condition[‘CREATE_TIME‘]=‘between 時間1 and 時間2‘ 這樣是不行的,花了一點時間查詢了下,yii2有這樣的方法:andFilterWhere,使用方法如下:

->andFilterWhere([‘like1‘, ‘name‘, ‘%a%‘])

#當(dāng)參數(shù)1,參數(shù)2為空時,between方法會自動過濾掉,也就是此條件會被刪除不執(zhí)行

->andFilterWhere([‘between‘, ‘created_at‘, 0(參數(shù)1), 1433088000(參數(shù)2)])

具體代碼如下:

if (!empty($params[‘id‘])) {

$where_condition[‘oid‘] = $params[‘id‘];

}

if (!empty($params[‘post_name‘])) {

$where_condition[‘post_name‘] = $params[‘post_name‘];

}

if (!empty($params[‘telephone‘])) {

$where_condition[‘telephone‘] = $params[‘telephone‘];

}

if (!empty($params[‘ticket_no‘])) {

$where_condition[‘ticket_no‘] = $params[‘ticket_no‘];

}

if ($params[‘status‘] != -1) {

$where_condition[‘status‘] = $params[‘status‘];

}

$start_date = $end_date = ‘‘;

if($params[‘isSearch‘] == 1) {

if (!empty($params[‘start_date‘]) && !empty($params[‘end_date‘])) {

$start_date = strtotime($params[‘start_date‘]);

$end_date = strtotime($params[‘end_date‘]) + 86400 - 1;

}

}

$where_condition[‘delete_flg‘] = 0;

$count = static::find()

->where($where_condition)

->andFilterWhere([‘between‘,‘CREATE_TIME‘,$start_date, $end_date])

->count();

總結(jié)

以上是生活随笔為你收集整理的yii2 mysql between_yii2:多条件多where条件下碰到between时,between语句如何处理呢?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。