Uses table alias in WHERE

If a table alias is used for the main table, it's not valid according to the SQL standard to refer to it by it's "real name" (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 table name. Now, checks for presence of a table alias and uses that if available.