首页Mysql删除数据delete from
Created At : 2021-11-13
Last Updated: 2022-01-10

Mysql删除数据delete from

从数据表中删除数据使用DELETE语句,DELETE语句允许WHERE子句指定删除条件。

delete语法

-- 单表删除
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [[AS] tbl_alias]
    [PARTITION (partition_name [, partition_name] ...)]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]
    
-- 多表删除
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    tbl_name[.*] [, tbl_name[.*]] ...
    FROM table_references
    [WHERE where_condition]

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    FROM tbl_name[.*] [, tbl_name[.*]] ...
    USING table_references
    [WHERE where_condition]
    
    
-- 例如
-- 条件删除
DELETE FROM somelog WHERE user = 'jcole'
ORDER BY timestamp_column LIMIT 1;

-- 多表删除
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;

如果想删除表中的所有记录,还可以使用TRUNCATE TABLE语句。

TRUNCATE将直接删除原来的表,并重新创建一个表,其语法结构为TRUNCATE TABLEtable_name。

TRUNCATE直接删除表而不是删除记录,因此执行速度比DELETE快。