Problem with NOT LIKE query in MySQL
i want to make a selection that excludes the entries that start with 07, 0256 and 0356. this is 开发者_开发问答what i tired:
SELECT * FROM rapoarte WHERE nrtel NOT LIKE '07%' OR nrtel NOT LIKE '0256%' OR nrtel NOT LIKE '0356%'
but it keeps selecting all the entries.
The condition nrtel NOT LIKE '0256%' OR nrtel NOT LIKE '0356%'
is always true (unless nrtel is NULL). You need to use AND:
SELECT * FROM rapoarte
WHERE nrtel NOT LIKE '07%'
AND nrtel NOT LIKE '0256%'
AND nrtel NOT LIKE '0356%'
Or you could rewrite it as follows if you find it easier to read:
SELECT * FROM rapoarte
WHERE NOT (
nrtel LIKE '07%' OR
nrtel LIKE '0256%' OR
nrtel LIKE '0356%'
)
The expression (NOT a) OR (NOT B)
is not the same as NOT (a OR b)
. See De Morgan's Laws.
精彩评论