'Extract additional fields from a joined table: if there are
fields in joined tables which haven\'t been extracted in the
master query, then we perform a separate query for each joined
table (this is faster than building a multi-table-join)
@param tablename: name of the joined table
@param query: the Query
@param fields: the fields to extract
@param records: the output dict to update, structure:
{RecordID: {ColumnName: RawValues}}
@param represent: store extracted data (self.field_data) for
fast representation, and estimate lookup
efforts (self.effort)
@return: the output dict'

def joined_query(self, tablename, query, fields, records, represent=False):

    s3db = current.s3db
    ljoins = self.ljoins
    table = self.resource.table
    pkey = str(table._id)
    sresource = s3db.resource(tablename)
    (efields, ejoins, l, d) = sresource.resolve_selectors([])
    tnames = (ljoins.extend(l) + list(fields['_left'].tables))
    sjoins = ljoins.as_list(tablenames=tnames, aqueries=self.aqueries)
    if (not sjoins):
        return records
    del fields['_left']
    extract = fields.keys()
    for efield in efields:
        fields[efield.colname] = efield.field
    sfields = [f for f in fields.values() if f]
    if (not sfields):
        sfields.append(sresource._id)
    sfields.insert(0, table._id)
    rows = current.db(query).select(left=sjoins, distinct=True, cacheable=True, *sfields)
    records = self.extract(rows, pkey, extract, records=records, join=True, represent=represent)
    return records
