MySQL Use Select on Multiple Conditions on Multiple Columns in a Table - mysql

I have a database with Name Database1 and Table named table1.The columns in the table are Client Name and Country.I want to search for clients based on a condition using LIKE and want to Exclude in results the Clients from NOT LIKE (NOT IN) Countries.
Client Name,Country
Sandeep,India
Mandeep,USA
John,Japan
Vinay,China
Amit,USA
etc...
I tried below stated query.It does give out results but does not filter or exclude the Not Like countries, rather displays all countries.
SELECT CLIENT NAME,
COUNTRY
FROM DATABASE1.TABLE1
WHERE (CLIENT NAME LIKE 'VINAY')
OR (CLIENT NAME LIKE 'AMIT')
AND COUNTRY NOT IN ('INDIA',
'JAPAN')
I have also tried with NOT LIKE:
SELECT CLIENT NAME,
COUNTRY
FROM DATABASE1.TABLE1
WHERE (CLIENT NAME LIKE 'VINAY')
OR (CLIENT NAME LIKE 'AMIT')
AND (COUNTRY NAME NOT LIKE 'INDIA')
AND (COUNTRY NAME NOT LIKE 'JAPAN')
Both of above gave results But without filtering-out/excluding India and China in results.Results are appearing for all countries in-spite of using Not LIKE / NOT IN
Any Solution or what is my mistake ?

Change in both queires:
Query 1:
SELECT CLIENT NAME,
COUNTRY
FROM DATABASE1.TABLE1
WHERE (CLIENT NAME LIKE 'VINAY' OR CLIENT NAME LIKE 'AMIT')
AND COUNTRY NOT IN ('INDIA', 'JAPAN')
Query 2:
SELECT CLIENT NAME,
COUNTRY
FROM DATABASE1.TABLE1
WHERE (CLIENT NAME LIKE 'VINAY' OR CLIENT NAME LIKE 'AMIT')
AND COUNTRY NAME NOT LIKE 'INDIA' AND COUNTRY NAME NOT LIKE 'JAPAN'

AND has higher precedence than OR, so your first filter is equivalent to:
(CLIENT NAME LIKE 'VINAY') OR (
(CLIENT NAME LIKE 'AMIT') AND COUNTRY NOT IN ('INDIA','JAPAN')
)
Similarly, your second filter is equivalent to:
(CLIENT NAME LIKE 'VINAY') OR (
(CLIENT NAME LIKE 'AMIT') AND
(COUNTRY NAME NOT LIKE 'INDIA') AND
(COUNTRY NAME NOT LIKE 'JAPAN')
)
To specify alternative precedence, you should include explicit parentheses:
(CLIENT NAME LIKE 'VINAY' OR CLIENT NAME LIKE 'AMIT')
AND COUNTRY NOT IN ('INDIA','JAPAN')
(Note that wrapping individual terms in their own parenthesis contributes nothing, so I have removed them).
Note also that your expression can be abbreviated using IN() for the OR terms:
CLIENT NAME IN ('VINAY','AMIT') AND COUNTRY NOT IN ('INDIA','JAPAN')

Related

MySQL match a value with wildcard in multiple columns

I have a teachers table that looks like this:
teacherid
teacherfname
teacherlname
salary
1
Alexander
Bennett
55.30
I would like to return any record that contains a given string in the teacherfname, teacherlname and salary columns.
What I have right now (this returns exact match only):
SELECT * FROM `teachers` WHERE 'Alexander' IN (teacherfname, teacherlname, salary)
What I would like to do is something like this (this would not return anything):
SELECT * FROM `teachers` WHERE '%Alex%' IN (teacherfname, teacherlname, salary)
What do I need to make the query work? Thank you.
I would assume that the value %Alex% won't ever match the salary column. If you want to search for any rows where the first name or last name include "Alex" I would use simple pattern matching, and force all comparisons to use the same letter case.
For example:
SELECT *
FROM `teachers`
WHERE lower(teacherfname) like '%alex%'
or lower(teacherlname) like '%alex%'

Mysql LIKE is not working when there is & in data

select name from students where name like '%A&M%';
above query is not giving results whose name contains A&M instead it giving whose name as A(any character)M in name
but when i run this select name from students where name like '%A&MOhan%'; it is giving students whose name contains A&Mohan
I am using hibernate createNativeQuery
You need to escape the input for special characters with a backslash, so for example:
select name from students where name like '%A\&M%';
Should give you the correct result

MySQL - Filter based on the first character of a column in a select query

Names:
Watts, Odysseus D.
Schultz, Wilma D.
Mckinney, Madaline R.
Charles, Mohammad O.
My table names like this structure, first_name and last_name are in single column called names, i want to get all names from starting with letter 'M' in their last name,
How to write in mysql query for this criteria???
If the last name appears at the beginning of the string, the you can just do:
select * from names where name like 'M%'
On the other hand, if you want to filter on the first character after the comma, then:
select * from names where name like '%, M%'

search for substring in string mysql

i have two tables of businesses , i need to select(search) between tables by business name.
for example: if in one table i have business with name "Alcantara furniture network" with id 1, in another table the name "Alcantara furniture" with id 2 . the id 2 should be returned when searched from the first table with id 1.
i have tried to write query with LIKE but it is not helpful because it won't return the other business(id 2) . how can i search a substring in a string in mysql ?
You should change your like query to select what is common about the 2 values, i.e. 'Alacantara furniture':
select bussid,name
from tempBusiness
WHERE name LIKE '%Alcantara furniture%'
the string 'network' only appears in one of the names, so WHERE name LIKE '%Alcantara furniture network%' will only match one.

Mysql search in database

I have name,surname,city,address,mobile fields in mysql table.
I want a select statement that select all the records if any of the above fields data matches;
For eg: If i put name: yusuf. then select statement should look data :yusuf in all column mentioned above and if any of the record matches then it should show the result.
Lets say you search by name.
Then use SELECT * FROM table WHERE name LIKE userinput.
Hope this help
select name,surname,city,address,mobile from Table
where name like '%Yusuf%'
Or Surname like '%Yusuf%'
or City like '%Yusuf%'
Or Address like '%Yusuf%'
Or Mobile like '%Yusuf%'