Query determined by SQL ID

0

I have a question that I am asking, the fact is that I am very bad at doing it.

My table: payments_credit With its columns: credit_pay_id, contract_id, acquisition_date, credit_credit.

I try to get the LAST "id_pagos_credito" from my "contrato_id" (so far so good with my current query) but I need to show me the record of any of the other columns, for example: the value of "Cost_credit". What I wear:

SELECT MAX(id_pagos_credito) FROM pagos_credito WHERE contrato_id=1

id_pagos_credito | contrato_id | date_acquisition | cost_credit

1 | 1 | 2018-04-11 | 100

2 | 2 | 2018-04-09 | 50

3 | 1 | 2018-04-08 | 20

4 | 2 | 2018-04-10 | 200

I would like you to show me when I run the query (cost_credito de contrato_id) of 2: 200 (date_acquisition of contract_id) of 1: 2018-04-08

Well, it's basically selecting the last value (not the greatest) of X table based on your ID. I hope to make myself understood, this is the basis to be able to pass it to codeigniter by passing parameters. Thanks

    
asked by González 07.04.2018 в 08:21
source

1 answer

5

To find the "last value" we order the column with ORDER BY id_pagos_credito followed by ASC or DESC to indicate the address of the order, and then to limit to a single result LIMIT 1

Remaining as a result:

SELECT id_pagos_credito, costo_credito
FROM pagos_credito
WHERE contrato_id=1
ORDER BY pagos_credito.id_pagos_credito DESC
LIMT 1;
    
answered by 07.04.2018 / 08:44
source