Doctrine查询命令

时间:2011-11-08 19:52:14

标签: sql doctrine dql

我有一个查询,我想订购一个附加的子选择

$this->queryObj = Doctrine_Query::create()
    ->select('l.*')
    ->addSelect('(SELECT count(*) FROM LeagueMatch m LEFT JOIN m.UserA a LEFT JOIN m.UserB b WHERE m.league_id = ? AND m.user_id_a=l.user AND m.result_accepted=1 AND m.isOneOnOneMatch = true OR m.league_id = ? AND m.user_id_b=l.user AND m.isOneOnOneMatch = true AND m.result_accepted=1) as numberofmatches')
    ->addSelect('(SELECT sum(m4.result_a) FROM LeagueMatch m4 
                  LEFT JOIN m4.UserA a4 
                  LEFT JOIN m4.UserB b4
                  WHERE m4.league_id = ? AND m4.user_id_a=l.user AND m4.isOneOnOneMatch = true AND m4.result_accepted=1) as winresults1')
    ->addSelect('(SELECT sum(m5.result_b) FROM LeagueMatch m5 
                  LEFT JOIN m5.UserA a5 
                  LEFT JOIN m5.UserB b5
                  WHERE m5.league_id = ? AND m5.user_id_b=l.user AND m5.isOneOnOneMatch = true AND m5.result_accepted=1) as winresults2')
    ->where('l.league = ?', $id)
    ->from('LeagueUser l')->orderBy('(winresults1 + winresults2) DESC')->execute(array(HERE_ARE_MY_VALUES));

这部分“orderBy('(winresults1 + winresults2)”不起作用,我不知道为什么。 它没有显示我的错误,语法没问题,但我的结果不是wresult 1 + 2的订单

1 个答案:

答案 0 :(得分:0)

试试这个。不确定它是否有效,但发生在我身上;)

$this->queryObj = Doctrine_Query::create()
    ->select('l.*')
    ->addSelect('(SELECT count(*) FROM LeagueMatch m LEFT JOIN m.UserA a LEFT JOIN m.UserB b WHERE m.league_id = ? AND m.user_id_a=l.user AND m.result_accepted=1 AND m.isOneOnOneMatch = true OR m.league_id = ? AND m.user_id_b=l.user AND m.isOneOnOneMatch = true AND m.result_accepted=1) as numberofmatches')
    ->addSelect('(SELECT sum(m4.result_a) as total FROM LeagueMatch m4 
                  LEFT JOIN m4.UserA a4 
                  LEFT JOIN m4.UserB b4
                  WHERE m4.league_id = ? AND m4.user_id_a=l.user AND m4.isOneOnOneMatch = true AND m4.result_accepted=1) as winresults1')
    ->addSelect('(SELECT sum(m5.result_b) as total FROM LeagueMatch m5 
                  LEFT JOIN m5.UserA a5 
                  LEFT JOIN m5.UserB b5
                  WHERE m5.league_id = ? AND m5.user_id_b=l.user AND m5.isOneOnOneMatch = true AND m5.result_accepted=1) as winresults2')
    ->where('l.league = ?', $id)
    ->from('LeagueUser l')->orderBy('(winresults1.total + winresults2.total) DESC')->execute(array(HERE_ARE_MY_VALUES));