日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

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

yii2:多條件多where條件下碰到between時(shí),between語(yǔ)句如何處理呢?

我有一張表:

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

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

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時(shí)間范圍查詢時(shí),那么問(wèn)題來(lái)了,between怎么加進(jìn)去?$where_condition[‘CREATE_TIME‘]=‘between 時(shí)間1 and 時(shí)間2‘ 這樣是不行的,花了一點(diǎn)時(shí)間查詢了下,yii2有這樣的方法:andFilterWhere,使用方法如下:

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

#當(dāng)參數(shù)1,參數(shù)2為空時(shí),between方法會(huì)自動(dòng)過(guò)濾掉,也就是此條件會(huì)被刪除不執(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语句如何处理呢?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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