[英]MySQL query for delete row if two columns are equal
How to delete all rows from a mysql table if values of two columns are equal 如果两列的值相等,如何从mysql表中删除所有行
Example Table 示例表
invoice_id| item_id | name | invoiced_qty | received_qty
---------------------------------------------------------
| 1 | 1 | item1 | 3 | 2
| 2 | 2 | item2 | 5 | 5
| 3 | 1 | item3 | 4 | 3
| 4 | 2 | item4 | 2 | 2
| 5 | 1 | item5 | 5 | 5
After deleting table needs to retains 删除表后需要保留
invoice_id| item_id | name | invoiced_qty | received_qty
---------------------------------------------------------
| 1 | 1 | item1 | 3 | 2
| 3 | 1 | item3 | 4 | 3
The select query which i created is 我创建的选择查询是
SELECT * FROM table1 A
INNER JOIN table1 B ON A.item_id = B.item_id
AND A.invoice_id = B.invoice_id
AND A.invoiced_qty = B.received_qty
Thanks 谢谢
Why not just SQL Fiddle : 为什么不只是SQL Fiddle :
DELETE FROM table1
WHERE invoiced_qty = received_qty
Your edit does not change anything. 您的编辑不会更改任何内容。 He is the SQL Fiddle demonstrating your SELECT query. 他是演示您的SELECT查询的SQL Fiddle 。 According to your sample data A.invoice_id
will never equal B.invoice_id
. 根据您的样本数据, A.invoice_id
将永远不会等于B.invoice_id
。 So you will not get any results. 因此您将不会获得任何结果。
You could simply wrap your select statement and select values to be deleted by id, like this: 您可以简单地包装您的select语句并选择要通过id删除的值,如下所示:
DELETE FROM table1
WHERE item_id IN (SELECT item_id FROM table1 A
INNER JOIN table1 B ON A.item_id = B.item_id
AND A.invoice_id = B.invoice_id
AND A.invoiced_qty = B.received_qty)
however you should accept answer by Linger as it is more straightforward solution, mine was to indicate that if you have something selected usually you can wrap and delete. 但是,您应该接受Linger的回答,因为这是更直接的解决方案,我的意思是,如果选择了某些内容,通常可以包装和删除。
Try this : 尝试这个 :
DELETE FROM table1 A
INNER JOIN table1 B ON A.item_id = B.item_id
WHERE A.invoiced_qty = B.received_qty
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.