我想与Laravel Eloquent进行联合查询。 当我们进行 UNION 查询时,两个查询中的选定列数量应该相同。 要跳过该规则,我想选择 NULL 作为Column_name
, 但是 Laravel API 会自动将 NULL 替换为"Null",这会导致错误"Null
列不存在"。
如何从 Null 中删除这些自动添加的引号?
这就是我所拥有的: 第一个查询:
...->select("Calendars.*","Services.Id as IdService","Services.Name as ServiceName","NULL as Price")
第二个查询:
...->select("Calendars.*","Services.Id as IdService","Services.Name as ServiceName","PaidService.Price")
结果是:
...union (select `Calendars`.*, `Services`.`Id` as `IdService`, `Services`.`Name` as `ServiceName`, `NULL` as `Price` from `Calendars`
多谢!
考虑为此使用 DB::raw。它将阻止 laravel 修改语句并按原样解析它。
DB::raw("NULL as Price")
这将进行第一个查询
...->select("Calendars.*",
"Services.Id as IdService",
"Services.Name as ServiceName",
DB::raw("NULL as Price"))