Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda