> 文章列表 > thinkphp:数据库查询操作(like,in ,find_in_set,neq,<>,whereNotNull,whereOr,多条件查询)

thinkphp:数据库查询操作(like,in ,find_in_set,neq,<>,whereNotNull,whereOr,多条件查询)

thinkphp:数据库查询操作(like,in ,find_in_set,neq,<>,whereNotNull,whereOr,多条件查询)

一、根据变量进行模糊查询

字段order_number形如什么$handname什么的字样

'order_number' => ['like', '%' . $handname . '%']

$data['info'] = db::table('rep_info_base')
->where(['account_id' => $account_id,'order_number' => ['like', '%' . $handname . '%']]
)
->select();

二、查询条件为字段值需要在一个数组中

字段order_status只能是'再次维修', '等待配件'中的一个

'order_status', 'in', ['再次维修', '等待配件']

 $data['info'] = db::table('rep_info_base')
->where('order_status', 'in', ['再次维修', '等待配件'])
->select();

三、判断一个数据库中是否存在一个值

find_in_set

判断数据库表rep_record_fittings中,是否存在字段fittings_name的值为$fittings_name的

$isexit = db::table('rep_record_fittings')->where('find_in_set(:fittings_name,fittings_name)', ['fittings_name' => $fittings_name])->select();
if ($isexit) {echo '存在';
} else {echo '不存在';
}

四、查询时不相等的用法

表rep_worker_info中的字段id不等于$worker_id变量

db::table('rep_worker_info')
->where('id', 'neq', $worker_id[$i])
->select(); 

五、查询一个列不为空或者不为空零时的数据(whereNotNull)

对表info进行查询,当表中line这一列不为0或不为空时查询表中数据,

'<>'可以换成'neq'

$List = Db::table('info')->where(function ($query) {$query->where('line', '<>', 0)->whereNotNull('line');})->select();

六、查询一个列为空或者为零时的数据(whereOr)

对表info进行查询,当表中line这一列为0或为空时查询表中数据,

$List = Db::table('info')->where(function ($query) {$query->where('line', '=', 0)->whereOr('line',null);})->select();

七、查询一个列的数值等于一个变量,并且另外一列的数据不是为零就是为空

查询表wip_operation_plan

当列wip_entity_name为变量$wip_entity_name

并且另外一列begin_quantity为空,或者为0

查询满上述条件的operation_code列的所有值

$data = Db::table('wip_operation_plan')->where(['wip_entity_name'=>$wip_entity_name])->where(function ($query) {$query->where('begin_quantity',null)->whereOr('begin_quantity','=',0); // 判断另外一列是否为0})->column('operation_code');