简体   繁体   中英

Doctrine Query order by sum

I have a query where i want to order for a subselect which is an addition

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

This part "orderBy('(winresults1 + winresults2) " is not working and i do not know why. Its not showing me an error, the syntax is ok but my result is not order for wresult 1 + 2

Try this. Not sure that it works, but occurred me ;)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM