php - MySql 查询到 Postgres 查询。 Postgres 不支持 IF 条件

标签 php mysql sql postgresql laravel

我打算将我的 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/

相关文章:

php - 在 PHP 中获取日期和数字工作日

php - 如何在 MySQL 数据库中存储苗族字符

mysql - 尝试通过命令行导入 .sql 时 root 用户的访问被拒绝 (Vagrant)

MySQL 在 SELECT 中部分匹配

java - 'where clause' 中未知的列用户名

php - 将多个图像上传到每行 PHP 和 Mysqli 数据库

PHP时区转换问题

php - 复合项目的相似度得分

sql - 如何将一组记录汇总为一行,其中一列中只有一条记录包含文本,而其他所有记录均为空?

MySQL 创建 View : duplicate rows which has different value in 4 columns and not empty