如何在两个表中编写SQLite更新操作?

时间:2010-06-18 03:13:25

标签: sql sqlite sql-update

我正在使用SQLite,

TABLE A
(
 ID
 FileNAME
 FOLDERID
)

TABLE B
(
 FOLDERID
 FOLDERPATH
)

我想写一个SQL语句来删除A中的所有文件,其中Folder是C:\ ABC \的子文件夹;

如何在一个SQLite语句中创建它,这是最好的方法吗?

非常感谢!

2 个答案:

答案 0 :(得分:1)

以下工作正常:

create table table_a (

   id int,
   file_name varchar(300),
   folder_id int
);

create table table_b (
 folder_id int,
 folder_path varchar(300)
);

insert into table_a (id, file_name, folder_id) values (1, 'file1.txt', 1);
insert into table_a (id, file_name, folder_id) values (1, 'file2.txt', 1);
insert into table_a (id, file_name, folder_id) values (1, 'file2-1.txt', 2);
insert into table_b (folder_id, folder_path) values (1, 'c:\abc\somefolder\another');
insert into table_b (folder_id, folder_path) values (2, 'c:\abcNOT\somefolder\another');

delete
from table_a
where folder_id in (select folder_id from table_b where substr(folder_path, 1, 7) = 'c:\abc\');

select * from table_a;

答案 1 :(得分:0)

同样的想法,但有一些修改: SQLite现在识别参照完整性:

CREATE TABLE Table_a(     id int,     file_name varchar(300),     folder_id int,     FOREIGN KEY(folder_id)REFERENCES Table_b(folder_id) );

CREATE TABLE Table_b(     folder_id int,     folder_path varchar(300) );

CREATE TABLE Table_b(folder_id,folder_path)VALUES(1,'c:\ abc \ somefolder \ another'); CREATE TABLE Table_b(folder_id,folder_path)VALUES(2,'c:\ abcNOT \ somefolder \ another'); CREATE TABLE Table_a(id,file_name,folder_id)VALUES(1,'file1.txt',1); CREATE TABLE Table_a(id,file_name,folder_id)VALUES(1,'file2.txt',1); CREATE TABLE Table_a(id,file_name,folder_id)VALUES(1,'file2-1.txt',2);

DELETE FROM Table_a WHERE folder_id IN(SELECT folder_id FROM Table_b WHERE folder_path LIKE'c:\ abc \%');

SELECT * FROM Table_a;