0
Good morning,
I’m having trouble doing this search in mysql:
I would like to get all active users with their last trips (last registration):
Command used:
SELECT u.id, t.user_id, u.name, u.status, t.name as last_trip
FROM users u
LEFT JOIN trips t ON u.id = t.user_id AND t.id = ( SELECT MAX(*) FROM trips)
WHERE u.status = 'active';
And if I want to bring all the fields of the 2 tables, the Trips table being the last record?
– Helder Ferrari
SELECT u.*, t.*
FROM users u
LEFT JOIN trips t ON t.user_id = u.id
WHERE t.id = (SELECT MAX(tr.id) FROM trips tr WHERE tr.user_id = u.id AND u.status = "active")
– João Paulo M. Bandolin