Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何基于同一表中的另一列更新列_Sql_Sqlite - Fatal编程技术网

Sql 如何基于同一表中的另一列更新列

Sql 如何基于同一表中的另一列更新列,sql,sqlite,Sql,Sqlite,我对SQL这整件事还很陌生,不知道如何解决我的问题。我的表如下所示,主键是(id,name): 现在我想更新每个名称,使其包含一个与mime类型对应的后缀,以便最终看起来像这样: | bid | name | value | |=====|==========|============| | 1 | filename | foo.txt | | 1 | mime | text/plain | | 2 | filename | bar.png |

我对SQL这整件事还很陌生,不知道如何解决我的问题。我的表如下所示,主键是
(id,name)

现在我想更新每个名称,使其包含一个与mime类型对应的后缀,以便最终看起来像这样:

| bid |   name   |   value    |
|=====|==========|============|
|  1  | filename | foo.txt    |
|  1  | mime     | text/plain |
|  2  | filename | bar.png    |
|  2  | mime     | image/png  |
我有一个脚本,它从/etc/mime.type获取mime后缀关联,这样就涵盖了该部分。但是,我无法终生构造正确的SQL更新查询


任何帮助都将不胜感激!抱歉,如果这是重复的,我不太确定我要问什么。

嗯。您可以使用相关子查询:

update t
    set value = (value ||
                 (select (case when t2.value = 'text/plain' then '.txt'
                               when t2.value = 'image/png' then '.png'
                               else ''
                          end)
                  from t t2
                  where t2.id = t.id and 
                        t2.name = 'mime'
                 )
                )
    where name = 'filename';
update t
    set value = (value ||
                 (select (case when t2.value = 'text/plain' then '.txt'
                               when t2.value = 'image/png' then '.png'
                               else ''
                          end)
                  from t t2
                  where t2.id = t.id and 
                        t2.name = 'mime'
                 )
                )
    where name = 'filename';