MySQL DELETE语句

# DELETE语句
# DELETE语句用于删除记录
# DELETE [IGNORE] FROM 表名 [WHERE 条件1,条件2] [ORDER BY ...] [LIMIT ...];

# 删除10部门中,工龄超过20年的员工记录
DELETE FROM t_emp 
WHERE deptno=10 AND FLOOR(DATEDIFF(NOW(),hiredate)/365)>20 ;

# 删除20部门中工资最高的员工记录
DELETE FROM t_emp
WHERE deptno=20 
ORDER BY sal+IFNULL(comm,0) DESC;
LIMIT 1;

# DELETE 内连接
# DELETE 表1,... FROM 表1 JOIN 表2 ON 条件 [WHERE 条件1,条件2] [ORDER BY ...] [LIMIT]
# 删除SALES部门和改部门的全部员工记录
DELETE e,d
FROM t_emp e JOIN t_dept d ON e.deptno = d.deptno
WHERE d.dname="SALES";


# 删除每个低于部门平均底薪的员工记录
DELETE e
FROM t_emp e JOIN (SELECT deptno,avg(sal) as avg FROM t_emp GROUP BY deptno) t
ON e.deptno=t.deptno AND e.sal

 

你可能感兴趣的:(MySQL)