We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Query Fail - 1066 Not unique table/alias: 'g2'

Hi everyone. I'm trying get data with in the following way:

$query = "SELECT g.game_id, g.play_date, p.name, gp.gain, 
                    (SELECT SUM(gp2.gain) FROM \Multiple\Frontend\Models\GamePlayer gp2 
                        LEFT JOIN \Multiple\Frontend\Models\Game g2 ON (g2.game_id = gp2.game_id) 
                            WHERE g2.play_date <= '{$date}' AND gp2.player_id = gp.player_id) as total                    
          FROM \Multiple\Frontend\Models\GamePlayer gp
            INNER JOIN \Multiple\Frontend\Models\Game g ON g.game_id = gp.game_id
            INNER JOIN \Multiple\Frontend\Models\Player p ON p.player_id = gp.player_id
          WHERE g.play_date = '{$date}'
            GROUP BY gp.player_id
            ORDER BY total DESC"

        $query = new Query($query, \Phalcon\DI\FactoryDefault::getDefault());

        return $query->execute();

After add the part "(...)LEFT JOIN \Multiple\Frontend\Models\Game g2(...)" on query, i started to receive the follow error message:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'g2'

I tried add "as" before alias, rename alias, and nothing's ok. Someone can help-me? Thanks very much!

edited Jan '16

I have similar problem. $phql = "SELECT DISTINCT(s_d.id) as id, s_d.host FROM OffersDomains o_s_d, Domains s_d LEFT JOIN ZpDomainStatsUpdated AS d_u ON s_d.id = d_u.domain_id WHERE ( d_u.date_updated < '$weekAgoDate' OR d_u.date_updated IS NULL ) AND o_s_d.domain_id = s_d.id AND host != '' ORDER BY d_u.date_updated IS NULL DESC, s_d.id ASC LIMIT 1 ";

results in

Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'd_u''