Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
Reducing a certain amount from everyone's data in MySQL

I have a MySQL database. I want to reduce certain data using Cronjob at certain times of the week.

(Table name: Accounts)

username | hunger | hp
         |        |
Dworczyk | 100    | 100
Mateusz  | 100    | 95 

Let's say a random amount will be reduced from the hunger data of everyone in the table above. In other words, the hunger data of all the people above will be reduced.

const CronJob = require('cron').CronJob;
const { con, pool } = require("./container/mysql.js")

let random_number = Math.floor(Math.random() * 100) + 1;

let job = new CronJob('0 0 * * *', function() {
    pool.query(`SELECT * FROM Accounts WHERE username = 'Dworczyk'`, async (err, rows) => {
        if (err) throw err;
        pool.query(`UPDATE Accounts SET hunger = ${rows[0].hunger} - ${random_number}`);
    });
}, null, true, 'America/Los_Angeles');

job.start();

What I did above is simple subtraction from a single person. It shouldn't just be tied to "Dworczyk". How can I make transactions on everyone without using the WHERE key?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Subtract from the column itself rather than a variable.

Also, use parametrized queries rather than substituting variables into the SQL.

pool.query('UPDATE Accounts SET hunger = hunger - ?', [random_number], function(err, response) {
    if (err) throw err;
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!