我有下表order_details
:
|---------------------|------------------|------------------|
| order_id | sap_number | product_name |
|---------------------|------------------|------------------|
| 1 | 123 | earphones |
|---------------------|------------------|------------------|
| 2 | 123 | battery |
|---------------------|------------------|------------------|
| 3 | 456 | charger |
|---------------------|------------------|------------------|
| 4 | 789 | phone |
|---------------------|------------------|------------------|
如果产品是电池,我想删除所有具有相同sap_number的记录。在这种情况下,我希望删除具有order_id 1和2的记录。
我尝试过:
delete from order_details
where order_id in (
select order_id
from (
select order_id
from order_details
group by sap_number
having product = 'battery'
) t
)
但它仅删除第二条记录,而不删除第一条记录。
谢谢。
答案 0 :(得分:2)
您可以在下面尝试
delete from order_details
where sap_number in (
select sap_number
from order_details
where product_name= 'battery'
)
答案 1 :(得分:0)
尝试
delete from order_details where sap_number IN (select sap_number from (select * from order_details having product_name = 'battery') t);