简体   繁体   English

给定另一个表中的值的情况下删除一个表中的行-SQLite

[英]Deleting rows in one table given values in another table -SQLite

I am working in a database containing ship information. 我正在一个包含船舶信息的数据库中工作。

Table1 has frequently logged information on time, location and speed, including an ID for the vessels. 表1经常记录有关时间,位置和速度的信息,包括船只的ID。 Each vessel is logged several times in the database. 每个容器在数据库中记录多次。

Table2 has fewer data points, and the information contains the ID and the ship type. 表2的数据点较少,并且该信息包含ID和船型。 It looks something like this: 看起来像这样:

Table1: 表格1:

Time -  Speed - Location - ID

10   -   10  -   X    -     111

12  -   10  -   X    -     112

30  -   11  -   X   -      111

42  -   10  -   X   -      113

51  -   12  -   X    -     114

59  -   12 -   X   -      112

67  -   14  -   X   -      114

81  -   13  -   X   -      111

90  -   10  -   X    -     113

96  -   13  -   X     -    114

... ...

Table2: 表2:

Time - ID -  Ship_type

15 -  111-  1

27  -  113-  12

40 -   112 - 4

73  -  111 - 1

80  -  114 -  18

87  -  112 -  4

97  -  113 -  12

What I want is to delete all the rows in Table1 where the Shiptype is above 10 for the corresponding ID in Table2. 我要删除的是Table1中的所有行,其中Table2中对应ID的Shiptype大于10。 Say 1-10 are cargo ships and 10-20 are tankers, and I want all data on cargo ships from table one. 假设1-10是货船,而10-20是油轮,我想从表1中获得所有货船数据。 Desired output: 所需的输出:

Time -  Speed - Location - ID

10   -   10  -   X    -     111

12  -   10  -   X    -     112

30  -   11  -   X   -      111

59  -   12 -   X   -      112

81  -   13  -   X   -      111

Thanks 谢谢

Does that help 有帮助吗

Select * from table1 where id in (Select id from table2 where shiptype between 10 and 20)

same goes as sub query for delete statement 与删除语句的子查询相同

As per your Question you need to link all tables using ID which is comman in all tables. 根据您的问题,您需要使用所有表中的逗号ID链接所有表。

use can use left Join for that. 使用可以使用左Join

Select Query 选择查询

SELECT * FROM Table1 as t1 
LEFT JOIN Table2 as t2 ON t1.ID = t2.ID 
LEFT JOIN Table3 as t3 ON t1.ID = t3.ID
WHERE 1 <= t2.Ship_type AND t2.Ship_type <=10;

Delete Query 删除查询

DELECT FROM Table1 as t1 
LEFT JOIN Table2 as t2 ON t1.ID = t2.ID 
LEFT JOIN Table3 as t3 ON t1.ID = t3.ID
WHERE t2.Ship_type > 10;

Hope this works for you :) 希望这对你有用:)

select * from table1 where id in (select id from table2 where Shop_type > 9)

That will work for sure. 那肯定会起作用。 But you also can try doing joins. 但是您也可以尝试加入。 It usually works faster then subquery. 它通常比子查询更快。

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

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