如何将 SQL 查询转换为 PHP 并在 WHERE 子句中使用 AND 运算符

How to convert SQL query to PHP and use AND operator in WHERE clause

我有下面的 php 查询,我想计算 id 在一列中存在的次数

$empCount = DB::table('m_employee')->count('group_id')->where('group_id',$idHolder);

//tblname is m_employee; column name is group_id and my search variable is $idHolder

错误信息是:

{"message":"Call to a member function where() on integer",.......

对于这个例子,当我的$idHolder的值为1时,$empCount的结果应该是13,如果是2,结果就是3。 在那之后,我如何从这个查询中使用 AND 运算符:

DB::table('emp')->where('key',  $emp)->update([
   'id' => $id,
]);

where('key', $emp) AND ('monthyear', '06/2018') 一样,基于上方 cost_date 列中的日期(仅提取月份和年份)。我很难决定将其插入何处。

您可以简单地使用下面的查询来获取计数

$empCount = DB::table('m_employee')->where('group_id',$idHolder)->count();

对于 AND 运算符,您可以简单地链接 where 子句来模拟此

DB::table('emp')->where('key', $emp)->where(DB::raw("(DATE_FORMAT(cost_date,'%m-%Y'))"), '=', '06-2018')->update([ 'id' => $id, ]);