0

I need to change one column, based on two WHERE conditions. How to write this query correctly?

  UPDATE MyTable
    SET `group_aso` = "REPACK"
    WHERE
    `REPACK` = "YES" AND `LABEL` = "NO"
    OR
    SET `group_aso` = "LABEL"
    WHERE
    `REPACK` = "NO" AND `LABEL` = "YES"

1 Answers1

0

You can combine the criteria and use a conditional case expression, something like

update MyTable
  set group_aso = 
      case REPACK when 'YES' then 'REPACK' else 'LABEL' end
where (REPACK = 'YES' and LABEL = 'NO')
   or (REPACK = 'NO' and LABEL = 'YES')
Stu
  • 30,392
  • 6
  • 14
  • 33
  • Thank you very much for help. Another question is whether this query can be set as a procedure (stored/virtual). So that it updates the group_aso column on an ongoing basis after adding an entry? I'm trying to add a procedure but it keeps giving me an error and I don't know how to add it properly. – 1shot2killz Mar 14 '23 at 17:01
  • I would suggest asking a new question and include the error you are getting and the code you are trying. – Stu Mar 14 '23 at 18:22