Updates a cell in a specified row and unspecified column with content containing a specific string
P粉197639753
P粉197639753 2023-09-05 00:22:58
0
1
544
<p>I have a table with the following columns: name - course1 - course2 - course3. The two lines look like this: </p> <pre class="brush:php;toolbar:false;">John - physics - math - art Sara - math - chemistry - psychology</pre> <p>Now that John has been kicked out of math class, I want to replace "math" with "none" in his rows. </p> <p>When I looked for a solution, I found this: </p> <pre class="brush:php;toolbar:false;">UPDATE tableName SET `course1` = 'none' WHERE `name`='John' AND `course1`='math';</pre> <p>This might be useful if I knew which column of john "math" was recorded in. But the word can appear in any column. What I need is something like this: </p> <p>sql_query="Find the row where <code>name</code>='John', then find the column where we have the word 'math', just replace 'math' with 'none' there. </p> <p>Can you help me solve this problem? </p>
P粉197639753
P粉197639753

reply all(1)
P粉113938880

In this case, I think there is no other way than to evaluate each column, like this:

update
   my_table
set 
   course1 = if(course1 = 'math', 'none', course1),
   course2 = if(course2 = 'math', 'none', course2),
   course3 = if(course3 = 'math', 'none', course3)
where
   name = 'John';
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template