我正在学习laravel。我想使用 eloquent 在 laravel 中编写以下查询: select b.branch_name, w.branch_work_name from branches AS b, branch_work_metadata AS w, branch_work_lookup AS bw where b.branch_id = bw.branch_id AND w.branch_work_id = bw.branch_work_id上面的查询在 phpmysql 中完美运行我有每个表的模型如下:表:分支机构 --- 型号:分支机构表:branch_work_metadata --- 模型:BranchWorkMetadata表:branch_work_lookup --- 模型:BranchWorkLookup当我尝试在 laravel 中编写上述查询时,它给了我错误 $branch = DB::table('branches as b', 'branch_work_metadata as w', 'branch_work_lookup as bw') ->select('b.branch_name','w.branch_work_name') ->join('b.branch_id','=','bw.branch_id') ->join('w.branch_work_id','=','bw.branch_work_id') ->get(); 上面给了我错误Illuminate\Database\QueryExceptionSQLSTATE[42S02]: Base table or view not found: 1146 Table 'b.branch_id' doesn't exist (SQL: select `b`.`branch_name`, `w`.`branch_work_name` from `branches` as `b` inner join `b`.`branch_id` on `=` = `bw`.`branch_id` inner join `w`.`branch_work_id` on `=` = `bw`.`branch_work_id`)还尝试了以下操作:$branch = BranchWorkLookup::Join('branches.branch_id','=','branch_work_lookup.branch_id') ->Join('branch_work_metadata.branch_work_id','=','branch_work_lookup.branch_work_id') ->select( 'branches.branch_name', 'branch_work_name' ) ->get();给出错误Illuminate\Database\QueryExceptionSQLSTATE[42S02]: Base table or view not found: 1146 Table 'branches.branch_id' doesn't exist (SQL: select `branches`.`branch_name`, `branch_work_name` from `branch_work_lookup` inner join `branches`.`branch_id` on `=` = `branch_work_lookup`.`branch_id` inner join `branch_work_metadata`.`branch_work_id` on `=` = `branch_work_lookup`.`branch_work_id`)怎么了?无法找到解决方案。请帮忙。提前致谢。
1 回答
肥皂起泡泡
TA贡献1829条经验 获得超6个赞
您正在尝试将老式连接语法与 Laravel 的查询构建器使用的现代版本混合,这也是您应该使用的版本。试试这个:
$branch = DB::table('branches as b')
->select('b.branch_name', 'w.branch_work_name')
->join('branch_work_lookup as bw', 'b.branch_id', '=', 'bw.branch_id')
->join('branch_work_metadata as w', 'w.branch_work_id', '=', 'bw.branch_work_id')
->get();
需要明确的是,这是您应该使用的原始 MySQL 查询:
SELECT
b.branch_name,
w.branch_work_name
FROM branches AS b
INNER JOIN branch_work_lookup bw
ON b.branch_id = bw.branch_id
INNER JOIN branch_work_metadata AS w
ON w.branch_work_id = bw.branch_work_id;
- 1 回答
- 0 关注
- 107 浏览
添加回答
举报
0/150
提交
取消