如何在 Laravel 查询生成器中使用 MySQL SET 变量?

问题描述 投票:0回答:2

如何使用在 DB::statement() 中使用的 MySQL 变量。我在那里放置了两个变量,即 @num 和 @type。

我在 DB::statement() 之后有一个查询生成器,它将使用 @num 和 @type 变量。

运行时,两个变量都返回 NULL,因为它无法获取 MySQL 变量。

id 数字 类型
1 (空) (空)
2 (空) (空)
3 (空) (空)
4 (空) (空)
5 (空) (空)
6 (空) (空)
7 (空) (空)
8 (空) (空)
9 (空) (空)
10 (空) (空)

预期输出应该是:

id 数字 类型
1 1 2
2 1 2
3 1 2
4 1 2
5 1 2
6 1 2
7 1 2
8 1 2
9 1 2
10 1 2

这是实际代码:

enter image description here

enter image description here

php mysql sql laravel database
2个回答

0
投票

$statement = DB::statement("SET @running_sum=0"); $counts = DB::select("选择 CONCAT( HOUR(q.count_time), ':' , LPAD(MINUTE(q.count_time) DIV 15 * 15, 2, 0), date_format(q.count_time, '%p')) as 时间, q.计数, @running_sum:=@running_sum + q.count AS 运行 从 (
选择 计数(*) AS

count
, count_time 为
count_time
FROM 计数器 -- 哪里 count_date = '?' GROUP BY CONCAT( HOUR(count_time), ':' , LPAD(MINUTE(count_time) DIV 15 * 15, 2, 0), date_format(count_time, '%p')), count_time 按计数日期、计数时间排序 )q;” // , ["date_today" => Carbon::now()->format("Y-m-d")] );
enter code here

© www.soinside.com 2019 - 2024. All rights reserved.