0

I have an array of unique Ids like the following one:

const myIds = [2, 3, 4];

I also have a database like this one:

+-----+------+
| id  | seen |
+-----+------+
|   0 |    0 |
|   1 |    0 |
|   2 |    0 |
|   3 |    0 |
|   4 |    0 |
+------------+

I would like to update my database so that the seen column of all the ids in my array are set to 1. That would leave me with this:

+-----+------+
| id  | seen |
+-----+------+
|   0 |    0 |
|   1 |    0 |
|   2 |    1 |
|   3 |    1 |
|   4 |    1 |
+------------+

I have tried this code but it only updates one entry:

    db.query('UPDATE table SET seen = 1 WHERE id = ?', myIds,
    function (error, results) {
        if (error) throw error;
        console.log("ok");
    });

Is there any other way to do so ?

Thanks for your help.

2 Answers 2

1

Try this:

db.query('UPDATE `table` SET seen = 1 WHERE id IN (?)', [myIds.join()],
    function (error, results) {
        if (error) throw error;
        console.log("ok");
    }
);
Sign up to request clarification or add additional context in comments.

1 Comment

I am having this error: Error: ER_TRUNCATED_WRONG_VALUE: Truncated incorrect DOUBLE value: '2,3,4'
1

Thanks @cdaiga I managed to make it work:

db.query('UPDATE table SET seen = 1 WHERE id IN (?)', [myIds],
    function (error, results, fields) {
        if (error) throw error;
        console.log("ok");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.