E
E
Eugene2020-01-30 17:20:37
MySQL
Eugene, 2020-01-30 17:20:37

Conditions in mysql?

there is a table with entries
option 1
setup1 0
setup2 0

how to make a query in mysql
if option is 1 then there is nothing to do
if option is 0 then write values ​​1 to setup1 and setup2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Athanor, 2020-01-30
@Athanor

Good afternoon, Eugene.
Let's say your table's DDL is:

CREATE TABLE settings (
  caption VARCHAR(255) UNIQUE NOT NULL, -- принимает значения option, setup
  value INT NOT NULL DEFAULT 0
);

Also, I assume that the values ​​of the first column are unique in the table.
The easiest way to solve your problem is this query:
UPDATE settings S1, settings S2
SET S1.value = 1
WHERE 
  S1.caption IN ('setup1', 'setup2')
  AND S2.caption = 'option' AND S2.value = 0;

Please note that the "caption" field is declared with the UNIQUE flag, this will add an index and, if there are a lot of values ​​in the table, will speed up the query.
I ask you to clarify whether it is required to clarify the mechanism for executing the request.
Sincerely,
Oleg Raev
CIO of Athanor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question