NOT LIKE clause is not working - mysql

Here is my query:
SELECT *
FROM client_info
WHERE (cid LIKE 'G%' OR cid LIKE 'H%' OR cid LIKE 'J%')
AND (client_name NOT LIKE 'P4%' OR client_name NOT LIKE 'P5%')
The results still contain client names starting with P4 and P5. What is wrong with NOT LIKE clause?

Change the second set's OR to an AND.
AND (client_name NOT LIKE 'P4%' AND client_name NOT LIKE 'P5%')

Others have given you the correct answer - you need to use AND instead of OR. But it's worth understanding why. Take a client named P5_JONES, for instance - why is this patient showing up in the list? Because its name is NOT LIKE 'P4%'. Yes, it is like P5%, but with an OR in there, only one of those expressions needs to be true to satisfy the condition.

you have to use AND instead of OR in NOT LIKE condition

Try this:
SELECT *
FROM client_info
WHERE (cid LIKE 'G%' OR cid LIKE 'H%' OR cid LIKE 'J%')
AND (client_name NOT LIKE 'P4%' AND client_name NOT LIKE 'P5%')
You must use AND and not OR operator. Because you use NOT LIKE so, you must check either condition.

Related

Mysql - how to use like on multiple columns

I've searched in mysql query a word in multiple column in java program. The number of column is variable.
It is correct this query:
select * from customer with (city, name) like%'adelaide'%
You can use CONCAT() function:
select * from customer WHERE concat(city,name) like '%adelaide%'
You can add as many columns to the concat function as you like.
Also as you see I changed your like syntax, '%WORD%' and used a simple where clause.
It's better to use CONCAT_WS() function.
CONCAT() returns NULL if any argument is NULL.
CONCAT_WS() skip any NULL values after the separator argument.
multiple like statements can not be used with or directly. You have to use column name for each like statement.
Use multiple like as mentioned below.
Select *
from animals
where
(
[animalscolumn] like ('%dog%') or
[animalscolumn] like ('%cat%') or
[animalscolumn] like ('%gerbil%') or
[animalscolumn] like ('%hamster%') or
[animalscolumn] like ('%guinea pig%')
)
select * from customer with city like%'adelaide'% or name like%'adelaide'%

how do i write a sql query to select the names that may contain any one of the vowels?

I m trying to query a database with about 2000 entries. I want to select the entries in which the names may contain any one of the vowel.
I tried using the following query, but it gives me those entries that contain all the given characters in that order.
select * from myTable where name like '%a%e%i%';
How do I modify the above query to select those entries with names that may contain at least anyone of the vowels.
Try this for SQL Server:
SELECT * FROM myTable WHERE name LIKE '%[AEIOU]%';
I hope this helps you...
SELECT * FROM myTable WHERE name REGEXP 'a|e';
or.....
SELECT * FROM myTable WHERE name REGEXP 'a|e|i';
In SQL Server, you would do:
where name like '%[aeiou]%';
In MySQL, you would do something similar with a regular expression.
Use OR like this.
This will work for both SQL Server and MySql.
select * from myTable where name like '%a%' OR name like '%e%' OR name like '%i%';
Use LIKE and OR.
Query
select * from myTable
where name like '%a%'
or name like '%e%'
or name like '%i%'
or name like '%o%'
or name like '%u%'

Can I use star notation in MySQL after a word, like med*

I have a database with columns with the same name and a number after it, like:
med1, med2, med3, med4..... etc.
Is it posible to do something like this:
Select medewerker.med*
FROM table
Instead of this?:
Select medewerker.med1,
medewerker.med2,
medewerker.med3,
medewerker.med4
FROM table
Nope. you can use LIKE in a where clause but definitely not when specifying the columns to select
Use this instead if the only fields are the ones specified above.
SELECT *
FROM table
if not you cannot use a LIKE statement on column SELECT's.
if you are refering to values inside the medewerker column then use
SELECT *
FROM table
WHERE medewerker LIKE 'med%'

mySQL LIKE operator that does not equal

How can i write a mySQL query where i am looking for something using the LIKE operator and NOT equaling what is LIKE?
select * from ets_fi where name like '%barcode&' <> '%barcode%';
NOT LIKE
select * from ets_fi where name NOT LIKE '%barcode%';
OR, if I misunderstood your intention, you may want:
select * from ets_fi where name LIKE '%barcode%' AND name != 'barcode';

SQL with multiple LIKEs over 1 variable

SELECT *
FROM company_address
WHERE address_telephone LIKE '%12%'
AND address_telephone LIKE '%45%' (edit)
Short question: Is there a way to only use the column name once?
(edit: It was an example, sorry for that.. )
If you really want to do it in one search, you can put your search criteria into a table variable and do a wildcard join on that:
DECLARE #t table (s varchar(20))
insert into #t values('12')
insert into #t values('45')
select ca.* from company_address ca inner join
#t t on ca.address_telephone like '%' + t.s + '%'
Your clause is faulty. address_telephone cannot be LIKE '%12%' without also being LIKE '%125%' so only the second of them is necessary anyway.
If this is only an example case and you didn't actually intend that WHERE logic, REGEXP might work for your situation:
WHERE address_telephone REGEXP '^.*125[0-9]+$'
Short answer: No
Side note: I do not know your business rules for the query, but it looks like you might want to be using OR instead of AND.
Long answer: If it was an equality, you could use IN. However, since you are using LIKE, you have to specify each LIKE condition.
in your example, yes:
SELECT *
FROM company_address
WHERE address_telephone LIKE '%125%'
explanation
if address_telephone LIKE '%125%' is true
then address_telephone LIKE '%12%' must be true as well, so there is no need to add it to the query
You can normally use wildcards etc to combine multiple likes into one statement. See below for a trivial example
declare #test varchar(50)
set #test = '123456789'
select 'true' where #test like '123%456%9'
Edit: That returns 'true', by the way
Maybe you're looking for a Levenshtein distance function? It should allow you to do a fuzzy search. There's one here.