【发布时间】:2017-12-18 13:50:34
【问题描述】:
ORM 的基本特性通常说是因为数据库迁移很容易从一个数据库迁移到另一个数据库。在这里,我最初在我的项目中使用了 MySQL 数据库,并计划将其迁移到 PostgreSQL。迁移时,它适用于几乎所有使用 select * from table、insert update 等的简单查询。
但是当我通过 MySQL 中的一些 datediff 函数时,ORM 无法在 PostgreSQL 中创建相应的查询。
这是我的查询
$result = DB::table('sales_target')
->select(db::raw('DATEDIFF(end_date,start_date) as DaysInQuarter'))
->whereraw("sales_target.target_quarter=$currentQuarter AND sales_target.target_year=$currentYear and status=1")
->first();
我想知道我是否用完全 ORM 结构编写了查询。请帮我解决这个问题。
编辑 1
简而言之,我想请任何人告诉我如何编写此查询
DB::table('sales_target')
->select(db::raw("DATEDIFF(end_date,start_date) AS DaysInQuarter"))
->first();
这应该可以在使用 ORM 的 mysql 和 postgresl 中工作。
在 Mysql 中它可以正常工作,但在 postgresql 中却不是。
我收到这样的错误
No function matches the given name and argument types. You might need to add explicit type casts. (SQL: select DATEDIFF(end_date,start_date) AS DaysInQuarter from "sales_target" limit 1)
【问题讨论】:
标签: postgresql laravel orm postgresql-9.1 laravel-eloquent