I have two tables named "Users" and "Payments". In Payment, there is a foreign key to the users table. This is a record of the payment schedule.
Select id,User_id from payment;
id | User_id |
---|---|
1 | 1 |
2 | 1 |
3 | 2 |
4 | 3 |
5 | 2 |
But my expected output is this.
id | User_id |
---|---|
1 | 1 |
3 | 2 |
4 | 3 |
I only want the first data for each User_id in SQL.
You can use the GROUP BY clause on the User_id column.
Try this...
Suppose, "first" refers to the smallest value of
id
in the payment table for that user, and you also need more details in the payment table or perform other operations on it...If you just want the MIN payment ID, then just do this: