id | name
-----------
1 | user1
2 | user2
3 | user3
id | user_id | email
----------------------
1 | 1 | mail1@yandex.ru
2 | 1 | mail2@yandex.ru
3 | 1 | mail3@mail.ru
4 | 2 | mail4@yandex.ru
5 | 3 | mail5@yandex.ru
6 | 3 | mail6@yandex.ru
user1
1. mail1@yandex.ru
2. mail2@yandex.ru
3. mail3@mail.ru
user2
1. mail4@yandex.ru
user3
1. mail5@yandex.ru
2. mail6@yandex.ru
$data = $db->query('SELECT u.name, e.email FROM users u, email e WHERE u.id = e.user_id')
->feachAll(PDO::FETCH_GROUP);
foreach($data as $name => $user) {
echo "$name\n";
foreach($user as $i => $email) {
echo "\t$i. $email\n";
}
}