如何将本机php转换为查询生成器laravel?

7ajki6be  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(549)

如何从原生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]:语法错误或访问冲突:1064您的sql语法有错误;检查与您的mariadb服务器版本相对应的手册,以获得正确的语法,以便在第1行使用“select unix\u timestamp(date(ts)*100 as ts)”、sum(pot+p1pot+p2pot+p3pot+p4p(sql:select select unix\u timestamp(date(ts)100 as ts)),sum(pot+p1pot+p2pot+p3pot+p4pot+p5pot+p6pot+p7pot+p8pot+p9pot)/count()作为moneyinplay from enginepoker_log . poker 分组依据 Date(ts) 订货人 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();

这是在叶片页

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

bihw5rsg1#

您要查找的查询应该更像这样:

$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 . "]";
}

相关问题