我打算将我的 laravel 应用程序数据库 mysql 更改为 postgres。我正在使用原始查询 这是我的查询:
select IF (modelno="" OR modelno IS NULL,name,concat(name, " / ",modelno)) as name, id from `models` where `deleted_at` is null order by `name` asc
当我将此查询用于 postgres 时,引发了以下异常。
zero-length delimited identifier at or near """" LINE 1: select IF (modelno="" OR modelno IS NULL,name,concat(name, '...
我的 larvel Controller 代码如下所示,并且可以与 MySql 一起正常工作:
DB::table('models')
->select(DB::raw('IF (modelno="" OR modelno IS NULL,name,concat(name, " / ",modelno)) as name, id'))
->orderBy('name', 'asc')
->whereNull('deleted_at')
->lists('name', 'id');
最佳答案
我认为 postgres 不支持 IF,所以试试 CASE :
DB::table('models')
->select(DB::raw('CASE WHEN modelno="" OR modelno IS NULL then name else concat(name, " / ",modelno) as name, id'))
->orderBy('name', 'asc')
->whereNull('deleted_at')
->lists('name', 'id');
关于php - MySql 查询到 Postgres 查询。 Postgres 不支持 IF 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625474/