简体   繁体   English

如何在 Laravel 中运行此 SQL 查询?

[英]How can I run this SQL query in Laravel?

I am trying to run this SQL query in many ways in laravel but it shows error.我试图在 Laravel 中以多种方式运行此 SQL 查询,但它显示错误。

SELECT * FROM reservas WHERE cliente_id = 2201 AND dia >= '2020-10-15' GROUP BY dia

I have tried to run this query but I get a violation error我已尝试运行此查询,但出现违规错误

DB::select('SELECT * FROM reservas WHERE cliente_id = :id AND dia >= :date GROUP BY dia', ['id' => $id, 'date' => $date]);

The variables that I send have data, that is, the error does not come from empty variables, far from it, but the error comes from the query.我发送的变量有数据,即错误不是来自空变量,远非如此,而是错误来自查询。 It should be noted that this query does work in SQL.应该注意的是,这个查询在 SQL 中确实有效。

You need to write your query like groupBy(DB::raw('dia'))您需要像groupBy(DB::raw('dia'))一样编写查询

\DB::table('reservas')->where('cliente_id',$id)->where('dia',$date)->get()->groupBy('dia');

You need to change false in config/database.php Like strict => false if your groupBy not working properly.如果您的 groupBy 工作不正常,您需要在config/database.php更改 false 就像strict => false

Hope its work!!希望它的工作!

@Cristian you need to use DB::raw inside DB::select @Cristian 你需要在DB::select 中使用DB::raw

using like that像那样使用

DB::select(DB::raw("SELECT * FROM reservas WHERE cliente_id = :id AND dia >= :date GROUP BY dia"), ['id' => $id, 'date' => $date]);

other wise used this if you don't want to set strict type false.其他明智的做法是,如果您不想将严格类型设置为 false。

DB::table('reservas')->where('cliente_id',$id)->where('dia',$date)->get()->groupBy('dia');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM