我想从3个表中选择一个加入Laravel5的表,但这是我第一次
public function getJD($id){
$result = [];
$result = JournalDetail::select('journal_detail.*, jdId as journal_detail, jdJ_id as journal_detail.journal_id .............,tranId as transactions_requiry,..............')
->join('journal_requiry','journal_requiry.id','=','journal_detail.journal_id')
->leftJoin('transactions_requiry','transactions_requiry.id','=','journal_requiry.tran_id')
->where('journal_detail.journal_id','=',$id)->get();
return $result;
}
错误,
QueryException in Connection.php line 624: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as `as` from `tb_journal_detail` inner join `tb_journal_requiry` on `tb_journal_' at line 1 (SQL: select `tb_journal_detail`.* as `as` from `tb_journal_detail` inner join `tb_journal_requiry` on `tb_journal_requiry`.`id` = `tb_journal_detail`.`journal_id` left join `tb_transactions_requiry` on `tb_transactions_requiry`.`id` = `tb_journal_requiry`.`tran_id` where `tb_journal_detail`.`journal_id` = 26)
但我不能这么做,我不知道怎么在拉瓦夫勒5。
请帮忙
最佳答案
在选择后放置DB::raw(
,这样您的查询将是:
$result = JournalDetail::select(DB::raw('journal_detail.*, jdId as journal_detail, jdJ_id as journal_detail.journal_id .............,tranId as transactions_requiry,..............'))
->join('journal_requiry','journal_requiry.id','=','journal_detail.journal_id')
->leftJoin('transactions_requiry','transactions_requiry.id','=','journal_requiry.tran_id')
->where('journal_detail.journal_id','=',$id)->get();
关于mysql - 如何使用select连接并在laravel5中指定任何字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35264955/