How to eliminate in two tables?

0

I am developing a fixed assets management system and I have two tables where there are low acquired assets, one tables is bajasaf and the other is distribdebajas . In bajasaf the information is saved as the id_baja, fecha_baja, cantidad_baja and in the table distribdebajas the id_baja, id_adquisicion, cantidad_rebajada y saldo_restante is saved, the saldo_restante is calculated for the same acquisition and in distribdebajas there can be several losses with different balances, until it reaches zero, as shown in the image.

I need to delete a low, for example this:

I also delete the rows with balance lower than the row that was removed in this case from the photo, it would be with the balance 2 and 0 and I would leave only the row with balance 12.

EYE: The elimination I do from the low table and it is in the form of CASCADE , so I delete the row with the same id_baja in distribdebajas , but I also need you to delete the rows as in the example explained.

    
asked by Felipe Toloza 27.02.2017 в 19:59
source

1 answer

0

You could use a subquery based on the low ID that you take as a reference. It's not a very elegant way but it works.

I'll give you the example

DELETE FROM
    prueba
WHERE
    id_baja IN (select id_baja from prueba WHERE saldo_adqui <= (select saldo_adqui from prueba where id_baja = 101))
    AND id_activo = (select id_activo from prueba where id_baja = 101)

You could also use a stored procedure or as mentioned by Weimar a Trigger.

    
answered by 27.02.2017 в 20:44