简体   繁体   English

SQLite:使用另一个查询的结果更新字段

[英]SQLite: Update field with result of another query

I have a database with one table, let's say 'tablename'. 我有一个包含一个表的数据库,比方说'tablename'。 Now, I want the first column of that table, named 'text' to be updated with the text it containts PLUS some new text which comes as result from another query executed over 'tablename2'. 现在,我想要该表的第一列,名为'text',用它所包含的文本更新PLUS一些新文本,这些文本来自在'tablename2'上执行的另一个查询。 So, I want something like this: 所以,我想要这样的东西:

UPDATE tablename SET text="current text" + (SELECT * FROM tablename2 where ID=12);

If 'text' value is ' Result not available ' I want to append ' here ', so that the field value is ' Result not available here ' 如果'text'值为' Result not available ',我想追加 ' here ',以便字段值为' Result not available here '

How is this possible? 这怎么可能? Thanks 谢谢

Try the concatenation operator (||): 尝试连接运算符(||):

UPDATE tablename SET text='current text'||(SELECT * FROM tablename2 where ID=12);

And I'm not sure, but you should use ' instead of " . 我不确定,但你应该使用'而不是'。

I probably misunderstood your question, but let's try: 我可能误解了你的问题,但试试吧:

http://sqlfiddle.com/#!5/959e5/2 http://sqlfiddle.com/#!5/959e5/2

UPDATE Table1
SET text = text || 
    CASE
      WHEN text = 'Result not available' THEN ' here'
      ELSE (SELECT text FROM Table2 where id = 12)
    END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM