Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My procedure
Java
CREATE procedure usp_set_night_free(booking_id bigint)
BEGIN

    SELECT b.price INTO @price_per_night FROM booked_rooms as b WHERE b.booking_id = booking_id LIMIT 1;

    UPDATE bookings as b
        JOIN booked_rooms b2 on b2.id = b.id
    SET b.total_price = b.total_price - @price_per_night
    WHERE b.id = b2.booking_id;
END;


Returns
Result consisted of more than one row


I am not really sure where the mistake could be, when I run the queries in the console separately I get -
1 row affected 
for both queries.

What I have tried:

I tried with LIMIT 1 but the problem is elsewhere.
Posted
Updated 22-Feb-22 21:14pm

1 solution

Your update query seems wrong. As per your schema design. 1 booking may have multiple booked rooms which is 1 to many relationships.

Below query will update the same booking multiple times because of 1 to many relationships. On top of that, your update is updating all bookings because you have not applied any filter criteria.

SQL
UPDATE bookings as b
JOIN booked_rooms b2 on b2.id = b.id
SET b.total_price = b.total_price - @price_per_night
WHERE b.id = b2.booking_id;


You need to change the above query to this

SQL
UPDATE bookings
SET total_price = total_price - @price_per_night
WHERE id = booking_id //booking_id input parameter
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900