gpt4 book ai didi

php - 如何将 native PHP 转换为查询生成器 Laravel?

转载 作者:搜寻专家 更新时间:2023-10-30 23:28:38 31 4
gpt4 key购买 nike

如何从原生 php 转换为查询构建器 laravel

$statsMoneyInPlay = array(); 
$sql_query = "SELECT UNIX_TIMESTAMP(Date(ts))*1000 As ts, sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay FROM enginepoker_log.poker WHERE GROUP BY Date(ts) ORDER BY Date(ts) LIMIT 30 ";

我已经制作了查询生成器,但仍然出现错误。这是错误

(2/2) QueryException 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 'SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts), sum(pot + p1pot + p2pot + p3pot + p4p' at line 1 (SQL: select SELECT UNIX_TIMESTAMP(Date(ts)100 as ts), sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count() As moneyInPlay from enginepoker_log.poker group by Date(ts) order by Date(ts) asc)

这是查询构建器:

$statsMoneyInPlay = DB::table('enginepoker_log.poker')
->selectRaw("SELECT UNIX_TIMESTAMP(Date(ts)*100 as ts)")
->selectRaw("sum(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay")
->groupBy("Date(ts)")
->orderBy("Date(ts)")
->get()
->toArray();

这是在 Blade 页面中

@php
foreach ($statsMoneyInPlay as $key => $value) {
echo "[".$value[0].", ".$value[1]."],";
@endphp

最佳答案

您正在寻找的查询应该看起来更像这样:

$statsMoneyInPlay = DB::table('enginepoker_log.poker')
->select(
DB::raw("UNIX_TIMESTAMP(Date(ts)*100) as timestamp"),
DB::raw("SUM(pot + p1pot + p2pot + p3pot + p4pot + p5pot + p6pot + p7pot + p8pot + p9pot) / count(*) As moneyInPlay")
)
->groupBy(DB::raw("DATE(ts)"))
->orderByRaw("DATE(ts)")
->get()
->toArray();

在 blade 中,你可以这样访问元素:

foreach($statsMoneyInPlay as $stat) {
echo "[" . $stat->timestamp . ", " . $stat->moneyInPlay . "]";
}

关于php - 如何将 native PHP 转换为查询生成器 Laravel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52600807/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com