我有以下表格:
book_tbl:
book_instance_id | book_type_id | library_instance_id | location_id | book_index 1 | 70000 | 2 | 0 | 1 2 | 70000 | 2 | 0 | 2 3 | 70000 | 2 | 0 | 3 4 | 70000 | 3 | 0 | 1 5 | 70000 | 3 | 0 | 2 6 | 70000 | 3 | 0 | 3 7 | 70000 | 4 | 1 | 1 8 | 70000 | 4 | 1 | 2 9 | 70000 | 4 | 1 | 3
和library_tbl:
library_instance_id | library_type_id | location_id 2 | 1000 | 0 3 | 1001 | 0 4 | 1000 | 1
我想更新book_type_id
中的字段book_tbl
仅针对library_type_id
1000
要检索此信息,我使用了sqlite查询:
SELECT * FROM ( ( SELECT *
FROM library_tbl
WHERE library_type_id=1000 ) t1
join book_tbl t2 on t1.location_id=t2.location_id
AND t1.library_instance_id=t2.library_instance_id
AND book_index=1 )
如何使用上面的查询和UPDATE查询来更新第1行和第7行:
UPDATE book_tbl SET book_type_id=15000 WHERE ????
答案 0 :(得分:1)
使用带有correlated subquery的EXISTS来检查相应的库行是否存在:
UPDATE book_tbl
SET book_type_id = 15000
WHERE EXISTS (SELECT 1
FROM library_tbl
WHERE library_type_id = 1000
AND location_id = book_tbl.location_id
AND library_instance_id = book_tbl.library_instance_id)
AND book_index = 1;