Laravel原始sql查询返回空数组

时间:2015-07-12 18:55:06

标签: php mysql arrays laravel laravel-5

我有一个非常复杂的SQL查询,当我在phpMyAdmin或类似的地方测试它时工作正常。

我正试图让它在laravel工作,但我很困惑。它总是返回空数组。

以下是我的尝试:

   $result = DB::select('SELECT sa.division, Sum(P) as games, Sum(W) as wins, 
Sum(D) as draws, Sum(L) as losses,SUM(F) as goals, SUM(A) as conceded, SUM(Pts) as  points FROM(SELECT gameday, division, home club, season, id, changes, penalties, ft, 1 P,
    IF (changes=2 OR penalties=2 OR (homegoals < awaygoals AND changes!=1 AND penalties!=1),1,0) W,
    IF (homegoals = awaygoals AND changes!=1 AND club!=2 AND penalties!=1 AND penalties!=2,1,0) D,
    IF (changes=1 OR penalties=1 OR homegoals > awaygoals,1,0) L,
    CASE WHEN changes=2 THEN 3 WHEN changes=1 THEN 0 ELSE awaygoals END F,
    CASE WHEN changes=2 THEN 0 WHEN changes=1 THEN 3 ELSE homegoals END A,
    CASE WHEN changes=2 OR penalties=2 OR (homegoals < awaygoals AND changes!=1) THEN 3 WHEN homegoals = awaygoals AND changes!=1 AND changes!=2 AND penalties!=1 AND penalties!=2 THEN 1 ELSE 0 END PTS
    FROM games) as tot, clubs s, series sa WHERE tot.club=s.id AND tot.division = sa.id AND s.url="awayteam" AND tot.ft=1 GROUP BY tot.division ORDER BY tot.division
    ');
    dd($result);

这正是Laravel docs告诉你这样做的原因。

我做错了什么?我会使用查询构建器,但我的查询太复杂了。

我的查询:

SELECT sa.division, 
       Sum(p)   AS games, 
       Sum(w)   AS wins, 
       Sum(d)   AS draws, 
       Sum(l)   AS losses, 
       Sum(f)   AS goals, 
       Sum(a)   AS conceded, 
       Sum(pts) AS points 
FROM  (SELECT gameday, 
              division, 
              home club, 
              season, 
              id, 
              changes, 
              penalties, 
              ft, 
              1                                     P, 
              IF (changes = 2 
                   OR penalties = 2 
                   OR ( homegoals < awaygoals 
                        AND changes != 1 
                        AND penalties != 1 ), 1, 0) W, 
              IF (homegoals = awaygoals 
                  AND changes != 1 
                  AND club != 2 
                  AND penalties != 1 
                  AND penalties != 2, 1, 0)         D, 
              IF (changes = 1 
                   OR penalties = 1 
                   OR homegoals > awaygoals, 1, 0)  L, 
              CASE 
                WHEN changes = 2 THEN 3 
                WHEN changes = 1 THEN 0 
                ELSE awaygoals 
              end                                   F, 
              CASE 
                WHEN changes = 2 THEN 0 
                WHEN changes = 1 THEN 3 
                ELSE homegoals 
              end                                   A, 
              CASE 
                WHEN changes = 2 
                      OR penalties = 2 
                      OR ( homegoals < awaygoals 
                           AND changes != 1 ) THEN 3 
                WHEN homegoals = awaygoals 
                     AND changes != 1 
                     AND changes != 2 
                     AND penalties != 1 
                     AND penalties != 2 THEN 1 
                ELSE 0 
              end                                   PTS 
       FROM   games) AS tot, 
      clubs s, 
      series sa 
WHERE  tot.club = s.id 
       AND tot.division = sa.id 
       AND s.url = "awayteam" 
       AND tot.ft = 1 
GROUP  BY tot.division 
ORDER  BY tot.division 

0 个答案:

没有答案