I have two tables, one named user and the other named payment. A user can have multiple payments.
example:
user 1 with 2 payments
user 2 with 5 payments
user 3 with 10 payments
user 4 with 7 payments
I have this query:
select * from user inner join payment on payment.user_id = user.id limit 2
The query will return only user 1 with his 2 payments.
But I want to return user 1 and user 2, each with their payments.
2
Answers
If I understand well, you want to return the payments of two users, if so, try this:
If you are running MySQL 8.0.14 or higher, that’s a good spot for a lateral join:
Notes:
you do want to use
order by
withlimit
(otherwise the database may not return consistent results); I assumed anpayment_date
column in thepayments
table for this purpose, so this gives you the two latest payments per user (for the record, your initial code has the same problem, and there is no guarantee that it will consistently return the same result when ran multiple times against the same dataset)for performance, consider an index on
payment(user_id, payment_date desc)
when joining multiple tables, it is good practice to enumerate the columns you want in the resultset, rather than using
*
– this clarifies the intent, and avoids conflicts when columns have the same name in different tablesuser
is a keyword in MySQL, hence a bad choice for a column name (users
is not, for example)In earlier versions of MySQL 8.0, an alternative is
row_number()
(which might scale less efficiently if there are many payments per user):