Sql 如何基于同一表中的另一列更新列
我对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 |
(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';