What about defining the template for your infowindow in your HTML code? Something like this
<script type="infowindow/html" id="infowindow_template">
<div class="MyInfoWindowClass" id="MyInfoWindowID">
<a href="#close" class="cartodb-popup-close-button close">x</a>
<div class="cartodb-popup-content">
<%= infowindow_content() %>
</div>
<div class="cartodb-popup-tip-container"></div>
</div>
</script>
Then, in your js code (silly fields, just an example):
function infowindow_content(content)
{
var field1;
var field2;
var field3;
var sql = new cartodb.SQL({user: 'youruser'});
// Add needed WHERE constraints here
sql.execute("select t1.field1, t2.field2, t3.field from t1, t2, t3 WHERE ...")
.done(function(data) {
if (data.rows && data.rows.length) {
// Fill field1, field2, field3 properly
// example: field1 = data.rows[0].field1
}
});
return "<div><p>" + field1 + + "</p><p>" + field2 + "</p><p>" + field3 + "</p></div>";
}
Not tested, but I think something like that should work.