MySQL学习笔记(21)——多表删除
多表删除
语法结构:
DELETE tb_name[.*] [,tb_name[.*]] ...
FROM table_references
[WHERE where_condition]
查出商品名称相同的goods_name:
SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING COUNT(goods_name)>=2;
利用多表删除和子查询删除名字重复的纪录:
DELETE t1 FROM tdb_goods AS t1
LEFT JOIN (SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING COUNT(goods_name)>=2) AS t2
ON t1.goods_name = t2.goods_name WHERE t1.goods_id > t2.goods_id;
如图:
使用SELECT * FROM tdb_goods;可以看到id的值只剩下20个,,删除了两个id值比较大的重复商品。