简体   繁体   中英

Sqlite Update query using a subquery

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".

what is wrong with the query?

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;

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

sounds like it took the 'as' after join for the joined tables so either put as in the (... as ...) or bring "ON COMMAND" before as! like this -> (table1 join table2 on table1.fiel = table2.field) as something

UPDATE TABLE test_test 
SET testconsent_id = 
   (SELECT testconsent_id FROM test_groupedconsent AS A, test_test AS B 
    WHERE A.patient_id = B.patient_id AND A.creation_date = B.A.creation_date)   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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