Laravel-Mysql从连接表中获取元素
我从与部门表相关的cars表中获取总的CAROwnerDepartment id 但我无法从cars表中了解问题 这是密码Laravel-Mysql从连接表中获取元素,mysql,laravel,Mysql,Laravel,我从与部门表相关的cars表中获取总的CAROwnerDepartment id 但我无法从cars表中了解问题 这是密码 $dept = DB::table('department') ->select('department.name', DB::raw('COUNT(cars.carOwnerDept) AS dept_count')) ->join('cars', 'department.id', '=', 'cars.carOwnerDept') ->select('
$dept = DB::table('department')
->select('department.name', DB::raw('COUNT(cars.carOwnerDept) AS dept_count'))
->join('cars', 'department.id', '=', 'cars.carOwnerDept')
->select('department.name', 'cars.issue')
->groupBy('department.name')
->where('cars.carType', '=', 'Internal Customer')
->whereDate('cars.created_at', '>=', $from)
->whereDate('cars.created_at', '<=', $to)
->get();
观点
数据库结构
汽车
身份证|车主身份证|问题
1 1条形码问题
2.3扫描仪问题
3.2条形码问题
系
id |名称
1质量保证
2页
3.试试这个
$dept = DB::table('department')
->join('cars', 'department.id', '=', 'cars.carOwnerDept')
->select('department.name', DB::raw('COUNT(cars.carOwnerDept) AS dept_count'), 'cars.issue')
->where('cars.carType', '=', 'Internal Customer')
->whereDate('cars.created_at', '>=', $from)
->whereDate('cars.created_at', '<=', $to)
->groupBy('department.name')
->get();
试试这个
$dept = DB::table('department')
->join('cars', 'department.id', '=', 'cars.carOwnerDept')
->select('department.name', DB::raw('COUNT(cars.carOwnerDept) AS dept_count'), 'cars.issue')
->where('cars.carType', '=', 'Internal Customer')
->whereDate('cars.created_at', '>=', $from)
->whereDate('cars.created_at', '<=', $to)
->groupBy('department.name')
->get();
您可以获得如下所示的车主部门数量
$dept = DB::table('department')
->select(DB::raw('(SELECT COUNT(carOwnerDept) FROM cars WHERE carOwnerDept=department.id) AS dept_count'))
->join('cars', 'department.id', '=', 'cars.carOwnerDept')
->select('department.name', 'cars.issue')
->where('cars.carType', '=', 'Internal Customer')
->whereDate('cars.created_at', '>=', $from)
->whereDate('cars.created_at', '<=', $to)
->get();
您可以获得如下所示的车主部门数量
$dept = DB::table('department')
->select(DB::raw('(SELECT COUNT(carOwnerDept) FROM cars WHERE carOwnerDept=department.id) AS dept_count'))
->join('cars', 'department.id', '=', 'cars.carOwnerDept')
->select('department.name', 'cars.issue')
->where('cars.carType', '=', 'Internal Customer')
->whereDate('cars.created_at', '>=', $from)
->whereDate('cars.created_at', '<=', $to)
->get();
我强烈建议您在模型上创建函数,这样就不需要输入原始查询。代码更少,更容易理解。我强烈建议您在模型上创建函数,这样您就不需要输入原始查询。代码更少,更容易理解。获取错误SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“cars.issue”SQL:选择department.name,cars.issue from department:@Suvin94您需要加入需要从中获取数据的表。答案已更新。获取错误SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“cars.issue”SQL:选择department.name,cars.issue from department:@Suvin94您需要加入需要从中获取数据的表。答案已更新。获取错误语法错误或访问冲突:选择列表的1055表达式3不在GROUP BY子句中,并且包含未聚合的列“cars.cars.issue”,该列在功能上不依赖GROUP BY子句中的列;这与sql_mode=only_full_group_by sql不兼容:select department.name,COUNTGetting a error语法错误或访问冲突:select list的1055表达式3不在group by子句中,并且包含未聚合的列“cars.cars.issue”,该列在功能上不依赖group by子句中的列;这与sql不兼容。\u mode=only\u full\u group\u by sql:select department.name,COUNT