使用子查询的 Sqlite 更新查询 [英] Sqlite Update query using a subquery

查看:48
本文介绍了使用子查询的 Sqlite 更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用表 test_groupedconsent, 的 id 值更新表 test_test 列 "testconsent_id",其中 test_test 中的 patient_idtest_groupedconsent 表中的 patient_id 匹配并且两个表中的 creation_date 也匹配.我正在使用以下查询,但出现错误 -- "near "as": syntax error".

I have to update table test_test column "testconsent_id" with the id value of table test_groupedconsent, where the patient_id in test_test and patient_id in test_groupedconsent table match and also creation_date in both table match. I'm using the below query but getting error -- "near "as": syntax error".

查询有什么问题?

Update test_test as Tinner join (select id,patient_id,creation_date from test_groupedconsent) as Aon A.patient_id = T.patient_id and A.creation_date = T.creation_dateset T.testconsent_id = A.id;

推荐答案

不能在 UPDATE 语句中直接使用联接.

You cannot use a join directly in an UPDATE statement.

您必须使用相关子查询来查找所需的值(在子查询中,您可以为所欲为,但在这种情况下,您甚至不需要联接):

You have to use a correlated subquery to look up the desired value (in the subquery, you can do whatever you want, but in this case, you don't even need a join):

UPDATE test_test
SET testconsent_id = (SELECT id
                      FROM test_groupedconsent
                      WHERE patient_id    = test_test.patient_id
                        AND creation_date = test_test.creation_date);

这篇关于使用子查询的 Sqlite 更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆