INSERT INTO test
VALUES
('aaa王昭君1', '王昭君1'),
('aaa王昭君2', '王昭君2'),
('aaa王昭君3', '王昭君3'),
('aaa王昭君4', '王昭君4'),
('aaa王昭君5', '王昭君5'),
('bbb王昭君6', '王昭君6'),
('ccc王昭君7', '王昭君7'),
('ccc王昭君8', '王昭君8'),
('ddd王昭君9', '王昭君9');
需求:需要删除code不是以aaa开头的行
- 使用 NOT EXISTS
DELETE FROM test t
WHERE NOT EXISTS (SELECT t.`code` WHERE t.`code` LIKE 'aaa%');
- 使用 NOT LIKE
DELETE FROM test t WHERE t.code NOT LIKE 'aaa%';