我想在Laravel上进行以下查询:
SELECT
table1.* ,
table1.colb as aaa
FROM
table1
LEFT jOIN table2 on table2.cola = table1.colb
这是Laravel数据库查询代码:
DB::table('table1')
->leftJoin('table2','table2.cola', '=', 'table1.colb')
->select('table1.* , table1.colb as aaa')
->get();
但它不起作用,我得到SQL语法错误。 这是Laravel使用上面代码生成的SQL错误:
select `table1`.* as `table1.colb`
from `table1`
left join `table2` on `table2`.`cola` = `table1`.`colb`
如何使用laravel方式修复此问题?
答案 0 :(得分:1)
您需要将每个参数传递给select
方法才能正确准备:
DB::table('TABLE1')
->leftJoin('TABLE2','TABLE2.COLA', '=', 'TABLE1.COLB')
->select('TABLE1.*', 'TABLE1.COLB AS AAA') // <- 2 separate params here
->get();