Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8530855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:21:03+00:00 2026-06-11T09:21:03+00:00

I have a column called balance in a table. the values represent real money.

  • 0

I have a column called balance in a table.
the values represent real money. I need to tansfer safely certain amount from one row to another.

so the value is subtracted from row 1 and added to row 2. if row 1 balance is lower than the value the whole transfer operation should fail and both row ramin unchanged.

Since i’m using innodb I’m able to use a transaction to wrap the selects and updates
but it would be nice to have an efficient atomic operation to prevent locks for long time rollbacks if something fails.

Is there an efficient way updating those two rows?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:21:05+00:00Added an answer on June 11, 2026 at 9:21 am

    I developed my own solution:
    Table name is Balance Columns are id and balance.
    Precondition for this to work source and dest ids exist

    UPDATE
      Balance
    RIGHT JOIN
      (SELECT id FROM (SELECT :srcId AS id UNION ALL SELECT :dstId AS id) as v WHERE EXISTS(SELECT id FROM Balance WHERE id = :srcId AND balance >= :value)) AS ids ON Balance.id = ids.id
    SET
      balance = balance + IF(ids.id = :srcId, -:value, :value)
    

    parameter syntax is “:< paramter >” for php PDO::prepare

    please check if any doupts

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Events table I have a column called Pos1, which contains IDs from
I have a column called RawData of datatype Image in my table. I need
In database table I have column called options . It has type of integer
I have a column called <locations> , I want to find all matching values
I have a column called views in my table A. I want to increment
I have a column called THE_VALUE in a table TABLE_A , that holds data
I have a computed column called Cost that returns money. I want to have
I have a column called price and all the values are TEXT so like
I have a column called CODE in a MySQL table which can be NULL.
I have a column on an SQL Server 2005 table called BIO - the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.