用thinkphp写一个查询语句 查询统计z_data_days表中datetime字段的年月等于2023-09时Ln_Meet_Standards=1的count值年月等于2023-09时Ln_Meet_Standards=0的count值年月等于2023-09时Ld_Meet_Standards=0的count值年月等于2023-09时Ld_Meet_Standards=
可以使用ThinkPHP的查询构建器来实现该查询语句。首先需要在控制器中引入模型类,然后使用查询构建器的方法进行查询。
use app\model\ZDataDays; // 假设模型类为ZDataDays
public function queryStatistics()
{
$count_2023_09_Ln_Meet_Standards_1 = ZDataDays::where('datetime', 'like', '2023-09%')
->where('Ln_Meet_Standards', 1)
->count();
$count_2023_09_Ln_Meet_Standards_0 = ZDataDays::where('datetime', 'like', '2023-09%')
->where('Ln_Meet_Standards', 0)
->count();
$count_2023_09_Ld_Meet_Standards_0 = ZDataDays::where('datetime', 'like', '2023-09%')
->where('Ld_Meet_Standards', 0)
->count();
$count_2023_09_Ld_Meet_Standards_0 = ZDataDays::where('datetime', 'like', '2023-09%')
->where('Ld_Meet_Standards', 0)
->count();
$count_2023_08_Ln_Meet_Standards_1 = ZDataDays::where('datetime', 'like', '2023-08%')
->where('Ln_Meet_Standards', 1)
->count();
$count_2023_08_Ln_Meet_Standards_0 = ZDataDays::where('datetime', 'like', '2023-08%')
->where('Ln_Meet_Standards', 0)
->count();
$count_2023_08_Ld_Meet_Standards_0 = ZDataDays::where('datetime', 'like', '2023-08%')
->where('Ld_Meet_Standards', 0)
->count();
$count_2023_08_Ld_Meet_Standards_0 = ZDataDays::where('datetime', 'like', '2023-08%')
->where('Ld_Meet_Standards', 0)
->count();
// 返回统计结果
return [
'count_2023_09_Ln_Meet_Standards_1' => $count_2023_09_Ln_Meet_Standards_1,
'count_2023_09_Ln_Meet_Standards_0' => $count_2023_09_Ln_Meet_Standards_0,
'count_2023_09_Ld_Meet_Standards_0' => $count_2023_09_Ld_Meet_Standards_0,
'count_2023_09_Ld_Meet_Standards_0' => $count_2023_09_Ld_Meet_Standards_0,
'count_2023_08_Ln_Meet_Standards_1' => $count_2023_08_Ln_Meet_Standards_1,
'count_2023_08_Ln_Meet_Standards_0' => $count_2023_08_Ln_Meet_Standards_0,
'count_2023_08_Ld_Meet_Standards_0' => $count_2023_08_Ld_Meet_Standards_0,
'count_2023_08_Ld_Meet_Standards_0' => $count_2023_08_Ld_Meet_Standards_0,
];
}
上述代码中,假设ZDataDays为模型类,通过where方法可以设置查询条件,count方法用于统计满足条件的记录数。最后返回一个包含统计结果的关联数组。根据实际情况,需要将模型类和返回结果的键进行适当修改。
原文地址: https://www.cveoy.top/t/topic/i5Me 著作权归作者所有。请勿转载和采集!