我遇到了麻烦,我有一个产品分类。我的问题是,如果类别为空且没有产品,我如何从数据库中删除类别。
我的代码:
$sql = "DELETE
FROM category AS a
INNER JOIN products AS b
ON a.cat_id = b.cat_id
WHERE a.user_id = ?
AND a.cat_id = ? LIMIT 1";
答案 0 :(得分:0)
我会使用not exists
运算符来查找products
表中[不]的类别:
DELETE
FROM category c
WHERE NOT EXISTS (SELECT *
FROM products p
WHERE c.cat_id = p.cat_id);
答案 1 :(得分:0)
DELETE FROM category AS a
Where a.cat_id = ?
And not exists(
Select 1
From products p
Where p.cat_id = a.cat_id
);
此查询匹配绑定变量上的cat I&f,并过滤掉那些具有相应产品的内容。