如何将本机PHP转换为查询生成器Laravel?

时间:2018-10-02 01:12:20

标签: php mysql database laravel

如何从本地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服务器版本的正确语法   在'SELECT UNIX_TIMESTAMP(Date(ts)* 100 as ts),sum(pot +   第1行的p1pot + p2pot + p3pot + p4p'(SQL:选择SELECT   UNIX_TIMESTAMP(Date(ts) 100 as ts),sum(pot + p1pot + p2pot + p3pot +   p4pot + p5pot + p6pot + p7pot + p8pot + p9pot)/ count()为   enginepoker_log组的pokerDate(ts)组的moneyInPlay   由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

1 个答案:

答案 0 :(得分:1)

您要查询的查询看起来应该像这样:

$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();

在刀片中,您可以通过以下操作访问元素:

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