我有桌子" table_film"和列" film_cat"如下图所示
我想删除film_cat中的所有id除了id 4,9,114,131(他们不同时包含4个数字,可能是4,9, - ,4,114, - ,4,9,131,...)
我试过但没有工作!
DELETE FROM `table_film` WHERE id NOT IN (SELECT * FROM table_film WHERE film_cat = '4' or film_cat='9' or film_cat='114' or film_cat='131')
DELETE FROM table_film where film_cat NOT IN (4,9,114,131)
DELETE FROM table_film WHERE film_cat != '4,9,114,131'
答案 0 :(得分:3)
当你有一个非常非常糟糕的数据表示时会发生什么。你应该使用一个联结表,film_categories
每部电影和每个类别一行。
在单个列中存储多个ID是个坏主意。为什么不好?
所以,你应该修复数据模型。
现在,有时我们会遇到其他人非常糟糕的设计欺骗行为。在这种情况下,您可以使用以下方法:
DELETE FROM table_film
WHERE find_in_set(4, film_cat) > 0 AND
find_in_set(9, film_cat) > 0 AND
find_in_set(114, film_cat) > 0 AND
find_in_set(131, film_cat) > 0 ;
注意:AND
也许应该是OR
。问题的确切逻辑尚不清楚。
答案 1 :(得分:0)
试试这个;)
DELETE FROM table_film WHERE film_cat NOT REGEXP '4|9|114|131'
DELETE FROM table_film WHERE film_cat NOT REGEXP '[[:<:]]4[[:>:]]|[[:<:]]9[[:>:]]|[[:<:]]114[[:>:]]|[[:<:]]131[[:>:]]'
或使用FIND_IN_SET
:
DELETE FROM table_film
WHERE FIND_IN_SET('4', film_cat) = 0
AND FIND_IN_SET('9', film_cat) = 0
AND FIND_IN_SET('114', film_cat) = 0
AND FIND_IN_SET('131', film_cat) = 0