Home > database >  What's wrong with my SQL query with UPDATE and multiple conditions?
What's wrong with my SQL query with UPDATE and multiple conditions?

Time:10-08

UPDATE
    `learners`
SET
    `expiration` = '2021-10-15'
WHERE
    `protocol` IS NULL AND `course_id` = 15;

I can't figure out why it's not working! Yet the following query returns me records, so they exist:

SELECT
    *
FROM
    `learners`
WHERE
    `protocol` IS NULL AND `course_id` = 15;

I point out that course_id is a foreign key. Could this have something to do with it?

No errors appear. It simply does not update anything

SOLVED https://stackoverflow.com/a/69483346/13568667

CodePudding user response:

If you want to check your query better use SQL below

SELECT
    *
FROM
    `learners`
WHERE
    `protocol` IS NULL AND `course_id` = 15 AND ` expiration` = '2021-10-15'

CodePudding user response:

I found the solution. I didn't expect this behavior, because I know that SQL_SAFE_UPDATES only intervenes when there are no WHERE clauses. Instead in this case it was necessary to act in the following way:

SET
    SQL_SAFE_UPDATES = 0;
UPDATE
    `learners`
SET
    `expiration` = '2021-10-15'
WHERE
    `protocol` IS NULL AND `course_id` = 15;
SET
    SQL_SAFE_UPDATES = 1;
  • Related