yii2 mysql between_yii2:多条件多where条件下碰到between时,between语句如何处理呢?
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)題。
- 上一篇: php回调函数求1+2+3+...+10
- 下一篇: 分布式mysql保持数据一致性_一种分布