简体   繁体   English

mysql根据另一个表中的值删除行

[英]mysql delete row based on value from another table

I have a table where I would like to delete rows with INNER JOIN where the WHERE clause is based on a value in another table. 我有一个表,我想用INNER JOIN删除行,其中WHERE子句基于另一个表中的值。 The primary_key in Table 2 is a foreign_key in Table 1. 表2中的primary_key是表1中的foreign_key

Table 1 表格1

table_1_id  |  customer_id  |  table_2_id
-----------------------------------------
     1      |      5        |       1 
     2      |      5        |       2 
     3      |      5        |       3 

Table 2 表2

table_2_id   |    value
-----------------------
   1         |      0 
   2         |      0 
   3         |      1 

I want to delete rows from Table 1 where the value in Table 2 is equal to 0 . 我想删除表1中表2中的value等于0 So in this example the DELETE statement should delete the first two rows in Table 1, since I use INNER JOIN to connect the two tables. 因此,在此示例中, DELETE语句应删除表1中的前两行,因为我使用INNER JOIN连接两个表。

I tried this but it doesnt exactly do what I want. 我试过这个,但它并没有完全按照我的意愿行事。 This statement always deletes all the rows in Table 1 instead of the first two only. 此语句始终删除表1中的所有行,而不是仅删除前两行。

DELETE t1
FROM Table1 t1
INNER JOIN Table2 t2 ON (t1.table_2_id = t2.table_2_id) 
WHERE t1.customer_id = '5' 
AND t2.value = '0'

You are thinking too complicatedly. 你的想法太复杂了。 You want to delete from table 1 where a condition is met: 您想要从满足条件的表1中删除:

delete from t1 where customer_id = 5 and t2_id in (select t2_id from t2 where value = 0);

I cannot replicate this behaviour: 我不能复制这种行为:

DROP TABLE IF EXISTS table1;

CREATE TABLE table1
(table_1_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,customer_id INT NOT NULL
,table_2_id INT NOT NULL
);

INSERT INTO table1 VALUES
(1,5,1),
(2,5,2),
(3,5,3);

DROP TABLE IF EXISTS table2;

CREATE TABLE table2
(table_2_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,value TINYINT NOT NULL
);

INSERT INTO table2 VALUES
(1,0),
(2,0),
(3,1);

SELECT * FROM table1;
+------------+-------------+------------+
| table_1_id | customer_id | table_2_id |
+------------+-------------+------------+
|          1 |           5 |          1 |
|          2 |           5 |          2 |
|          3 |           5 |          3 |
+------------+-------------+------------+
3 rows in set (0.01 sec)

SELECT * FROM table2;
+------------+-------+
| table_2_id | value |
+------------+-------+
|          1 |     0 |
|          2 |     0 |
|          3 |     1 |
+------------+-------+
3 rows in set (0.00 sec)

DELETE t1
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.table_2_id = t2.table_2_id
WHERE t1.customer_id = 5
AND t2.value = 0
;

SELECT * FROM table1;
+------------+-------------+------------+
| table_1_id | customer_id | table_2_id |
+------------+-------------+------------+
|          3 |           5 |          3 |
+------------+-------------+------------+
1 row in set (0.00 sec)

See. 看到。 Works just fine. 工作得很好。

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

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