My MySQL query does not work when I use a%

1

I have the following query:

SELECT calldate, src, dst 
FROM cdr 
WHERE src != '442%' AND src != '044%' LIMIT 50;

If you run it, but I do not know if it's really well done.

What I need to do is bring the marked data where the condition is that the src is different from 442 (here you have more numbers that's why I put the symbol%) and so with 004.

    
asked by Javier fr 04.07.2018 в 21:20
source

2 answers

4

Use not like to compare in negative strings with queries like.

SELECT calldate, src, dst 
FROM cdr 
WHERE src NOT LIKE '442%' AND src NOT LIKE '044%' LIMIT 50;

You can see more info here .

    
answered by 04.07.2018 / 21:23
source
1

Your search intent is correct. The problem is that the wildcard % only works the way you want to use it when using LIKE . When you use it in your query:

SELECT calldate, src, dst 
FROM cdr 
WHERE src != '442%' AND src != '044%' LIMIT 50;

You are literally looking for those different than a 442% and 044% , therefore you will get results as 4420000 because 442% is different from 4420000 . That is, the symbol % is being considered literally.

What you need (as well @Carmen mentions) is to use a LIKE . With LIKE you can use % to represent any number of characters so in this case use a LIKE '442%' , for example, you would get all those that start with 442 : 442 , 442000 , 442001 , etc.

But you need the inverse, so what you have to do is use a NOT in your query:

SELECT calldate, src, dst 
FROM cdr 
WHERE src NOT LIKE '442%' AND src NOT LIKE '044%' LIMIT 50;

What does it mean that you will get all those records in which src does not start with 442 or 044 .

    
answered by 04.07.2018 в 21:58