我有这样的查询
SELECT area FROM history_cost_estimation ORDER BY CAST(REPLACE(area, ' ', '') AS INT)
我正在尝试将此类查询转换为laravel查询构建器,但我收到了很多错误
这是我的尝试
App\Models\HistoryCostEstimation::orderBy("CAST(REPLACE(area, ' ', '') AS INT)",'asc')->count();
这是错误的示例
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as `)` asc' at line 1 (SQL: select count(*) as aggregate from `engineertec`.`history_cost_estimation` order by `CAST(REPLACE(area,` as `)` asc)
答案 0 :(得分:1)
我会使用DB facade并使用原始mysql
确保将数据库外观添加到控制器或运行此
的任何位置use Illuminate\Support\Facades\DB;
然后做
DB::table('history_cost_estimation')->select('area')->orderBy(DB::raw('CAST(REPLACE(area, ' ', ''), 'INT')));