Answer the question
In order to leave comments, you need to log in
How to LEFT JOIN th add only one term, and not any, but the largest in this field?
Let there be two tables:
Clients
-------
Ivan
Anna
Rita
Telephones
-------------
Ivan | 854
Ivan | 921
Rita | 321
Anna | 111
Only one number for each client should be added to the first tablet, and the largest among the numbers of this client.
That is, you need to get:
Ivan | 921
Anna | 111
Rita | 321.
Tell me, please, how to do this?
Answer the question
In order to leave comments, you need to log in
select
u.*,
ph.phone_max
from users u
left join (
select
user_id, max(phone) phone_max
from phones
group by user_id
) ph on u.user_id=ph.user_id
If you only want the phone number from the join table and nothing else, a subquery as a property using max.
select C.Name,
(select max(T.Phone) from Telephone T where T.Name = C.Name) Max_Phone
from Clients C
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question