Always use content_database_info to get the database info. For example:
$field1 = content_database_info(content_fields('yourfield1', 'yourtable')); $table1 = $field1['table']; $column1 = $field1['columns']['value']['column']; $field2 = content_database_info(content_fields('yourfield2', 'yourtable')); $table2 = $field2['table']; $column2 = $field2['columns']['value']['column']; if ($table1 == $table2) { $sql = "SELECT n.*, t1.$column1, t1.$column2 FROM {node} n "; $sql .= " INNER JOIN {$table1} t1 ON t1.nid = n.nid AND t1.vid = n.vid"; } else { $sql = "SELECT n.*, t1.$column1, t2.$column2 FROM {node} n "; $sql .= " INNER JOIN {$table1} t1 ON t1.nid = n.nid AND t1.vid = n.vid"; $sql .= " INNER JOIN {$table2} t2 ON t2.nid = n.nid AND t2.vid = n.vid"; } $sql .= " WHERE n.nid = %d";