SQL 如何使用 INNER JOIN 从表中删除数据 [英] SQL How to Delete Data from Table Using INNER JOIN

查看:116
本文介绍了SQL 如何使用 INNER JOIN 从表中删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从oc_products"表中删除所有没有特定类别 ID 的记录.我创建了一个 SELECT 查询,该查询使用 INNER JOIN 列出这些产品,因为类别位于单独的表中.

I'm trying to delete all records form the table "oc_products" that don't have a certain category ID. I created a SELECT query that lists those products using an INNER JOIN, since the categories are in a separate table.

我想不通的是如何使用 DELETE 函数删除显示的记录.

What I can't figure out is how to use the DELETE function to delete the shown records.

这是我的代码的样子:

DELETE oc_product
FROM oc_product
INNER JOIN oc_product_to_category ON oc_product.product_id = oc_product_to_category.product_id
WHERE oc_product_to_category.category_id = 343

它显示错误意外的关键字,(靠近 INNER JOIN)".

Its showing the error "Unexpected keyword, (near INNER JOIN)".

推荐答案

在第一行中将 .* 添加到 p.

Add .* to p in your first line.

试试:

DELETE p.* FROM oc_product p
INNER JOIN oc_product_to_category pc ON p.product_id = 
    pc.product_id
WHERE pc.category_id = 343

这篇关于SQL 如何使用 INNER JOIN 从表中删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆