Use table alias in WHERE clause when JOINing

If a table alias is used for the main table, it's not valid according to the SQL standard to refer to it by its "real name" anymore (see http://www.postgresql.org/docs/8.2/static/queries-table-expressions.html#QUERIES-TABLE-ALIASES).

This causes problems when doing JOIN with a table alias for the "main table", since previous code always used the table name. Now, the conditon code checks for presence of a table alias and uses that if available.