<?
$db="database";
$result = mysql_query("SELECT u.id, u.first_n, u.name, (SELECT fin.suma FROM $db.finances fin WHERE fin.stattya=5 GROUP BY fin.stattya) suma, fin.year, dfin.name
FROM
$db.users u
LEFT JOIN
$db.finances fin
ON u.id=fin.id_user
LEFT JOIN
$db.dov_finances dfin
ON fin.stattya=dfin.id
WHERE u.resid=1
GROUP BY u.id
ORDER BY u.first_n, dfin.id");
echo "<table>";
$i=0;
while ($sel_rezult=mysql_fetch_array($result)){
$i++;
$id_u=$sel_rezult[0];
$first_n=$sel_rezult[1];
$imya=$sel_rezult[2];
$user_name=$first_n.' '.$imya;
$suma=$sel_rezult[3];
$year=$sel_rezult[4];
$name=$sel_rezult[5];
echo "<tr>";
echo "<td align='center'>$i</td>";
echo "<td $color>$user_name</td>";
echo "<td align='center'>$suma</td>";
echo "</tr>";
}
echo "</table>";
?>