问题描述
我的 MysqL 中有一个名为 Animals 的表,其中的 description 列具有诸如
Id | 描述 | 年龄 |
---|---|---|
1 | 动物是猫 | 14 |
2 | 动物是狗 | 3 |
3 | 动物是老虎 | 5 |
4 | 动物是蝙蝠 | 12 |
5 | 动物是老鼠 | 8 |
6 | 动物是松鼠 | 13 |
7 | 动物是猫 | 4 |
8 | 狗 | 13 |
9 | 老虎 | 15 |
当描述以“Animal is ”开头且年龄大于或等于 12 时,我需要删除那些没有描述值 Cat、Bat 和 Squirrel 的行
这意味着,我只需要删除描述仅以“Animal is”开头的行,但我不想删除“Animal is cat”、“Animal is Bat”、“Animal is Squirrel”
Id | 描述 | 年龄 |
---|---|---|
1 | 动物是猫 | 14 |
4 | 动物是蝙蝠 | 12 |
6 | 动物是松鼠 | 13 |
8 | 狗 | 13 |
9 | 老虎 | 15 |
我试过了,
DELETE FROM animals WHERE description LIKE 'Animal is%' and Age >=12;
但这似乎删除了所有行,我想保留 cat、bat 和 Squirrel 的值。
解决方法
要选择您想要的记录,您可以使用以下条件:
age >= 12 AND
((description NOT LIKE 'Animal is%') OR (description LIKE '%Cat') OR
(description LIKE '%Bat') OR
(description LIKE '%Squirrel'))
因此,您可以通过将 NOT
应用于上述内容来删除所有其他行:
DELETE FROM animals
WHERE
NOT (
age >= 12 AND
((description NOT LIKE 'Animal is%') OR (description LIKE '%Cat') OR
(description LIKE '%Bat') OR (description LIKE '%Squirrel'))
)
,
我正在尝试过滤描述以“动物是”开头的行,然后从这些行中排除特定于猫、蝙蝠、松鼠的结果。请注意,“动物是猫猫”或“动物是猫蝙蝠”或“动物是猫猴”等列也将被删除。
DELETE FROM animals where description like 'Animal is%' and description NOT REGEXP '^Animal is Cat|Animal is Bat|Animal is Squirrel$' and age>=12;