Skip to content

Instantly share code, notes, and snippets.

@seliverstov-maxim
Created October 29, 2018 06:35
Show Gist options
  • Save seliverstov-maxim/246a26e720964d1ed7da4aab1dd55fb8 to your computer and use it in GitHub Desktop.
Save seliverstov-maxim/246a26e720964d1ed7da4aab1dd55fb8 to your computer and use it in GitHub Desktop.
Есть таблица users [ id (int), email (str), score (int), company_id(int) ]
со связью один ко многим с таблицей companies [ id(int), name(str) ]
получить выборку [ id, email, score, company_id ] с максимальным score по каждой компании
[Users]
ID | email | score | company_id
--------------------------------------------------------------------
1 | [email protected] | 8 | 1
2 | [email protected] | 8 | 2
3 | [email protected] | 4 | 1
4 | [email protected] | 1 | 2
5 | [email protected] | 2 | 1
6 | [email protected] | 3 | 2
7 | [email protected] | 0 | 1
8 | [email protected] | 6 | 2
9 | [email protected] | 9 | 1
[Result]
ID | email | score | company_id
--------------------------------------------------------------------
9 | [email protected] | 9 | 1
2 | [email protected] | 8 | 2
@seliverstov-maxim
Copy link
Author

Пожалуйста не комментируйте этот gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment