The query is as follows, there are 2 tables:
Pagos
-idPago
-idCliente
-fechaEnQuePaga
-fechaVencimiento
Cliente
-idCliente
-nombre
-apellidoPaterno
-apellidoMaterno
-correo
idCliente
is a foreign key.
Assuming the tables have the following data:
Table Payments:
1 | 1 | 2018-02-23 | 2018-03-23
2 | 2 | 2018-02-23 | 2018-05-23
3 | 2 | 2018-02-23 | 2018-06-23
4 | 1 | 2018-04-23 | 2018-07-23
Customers Table:
1 | Daniel | Sosa | Estrada | [email protected]
2 | Juan | Casas | Sola | [email protected]
And I want to get the client's data, followed by the payment details, but with the farthest expiration date, hoping to obtain:
1 | Daniel | Sosa | Estrada | 2018-04-23 | 2018-07-23
2 | Juan | Casas | Sola | 2018-02-23 | 2018-06-23
I have a query that resembles but does not give me the expected results:
SELECT
cliente.idcliente,
nombre,
apellidoPaterno,
apellidoMaterno,
fechaEnQuePaga,
fechavencimiento
FROM cliente, pagos
WHERE (cliente.idcliente = pagos.idcliente)
GROUP BY idcliente;
Result:
1 | Daniel | Sosa | Estrada | 2018-02-23 | 2018-03-25
2 | Juan | Casas | Sola | 2018-02-23 | 2018-05-23
I noticed that if I add MAX()
it locates the date to win farther, but it does not show its corresponding payment date:
SELECT
cliente.idcliente,
nombre,
apellidoPaterno,
apellidoMaterno,
fechaEnQuePaga,
MAX(fechavencimiento)
FROM cliente, pagos
WHERE (cliente.idcliente = pagos.idcliente)
GROUP BY idcliente;
Result:
1 | Daniel | Sosa | Estrada | 2018-02-23 | 2018-07-25
2 | Juan | Casas | Sola | 2018-02-23 | 2018-06-23
The payment date does not correspond to the one in the row.
I appreciate your response.