Ezekiel
Ezekiel

Reputation: 333

SQL select query not limiting to only one element in nodeJS

I'm struggling to select the ID of the last record in DB and alert it after insert. I have wrote and I have found that the best solution is to select descending with LIMIT . The query is fine, it works, but it selects each rows from last to the first, not only the last and I can't figure out why. Thanks !

app.post('/alert', function(req,res) {

    var query = "SELECT id FROM Control ORDER BY id DESC LIMIT 1";
    connection.query(query, function(error, result) {
            console.log(result);
            res.json(result);

    });
});

Upvotes: 0

Views: 469

Answers (1)

Luc
Luc

Reputation: 1491

You can use MAX:

app.post('/alert', function(req,res) {

var query = "SELECT max(id) as id FROM Control";
connection.query(query, function(error, result) {
        console.log(result);
        res.json(result);

});
});

Upvotes: 1

Related Questions