首页  >  问答  >  正文

指定行更新单元格,列未指定,以包含特定字符串的内容进行更新

<p>我有一个包含以下列的表格:name - course1 - course2 - course3。两行的样子如下:</p> <pre class="brush:php;toolbar:false;">John - physics - math - art Sara - math - chemistry - psychology</pre> <p>现在约翰被开除了数学课,我想在他的行中用“none”替换“math”。</p> <p>当我寻找解决方案时,我找到了这样的内容:</p> <pre class="brush:php;toolbar:false;">UPDATE tableName SET `course1` = 'none' WHERE `name`='John' AND `course1`='math';</pre> <p>如果我知道“math”是在约翰的哪一列记录的,那么这可能是有用的。但是这个词可以出现在任何一列中。我需要的是这样的东西:</p> <p>sql_query="找到 <code>name</code>='John' 的行,然后找到我们有单词'math'的列,只在那里将'math'替换为'none'。</p> <p>你能帮我解决这个问题吗?</p>
P粉197639753P粉197639753412 天前497

全部回复(1)我来回复

  • P粉113938880

    P粉1139388802023-09-05 11:25:34

    在这种情况下,我认为除了评估每一列之外,没有其他办法,就像这样:

    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';

    回复
    0
  • 取消回复