Mysql LIKE is not working when there is & in data - mysql

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

Related

Write a query which will show all employees whose last names start with "M" and have an 'ae' anywhere in their last name

I tried to write it like this.
WHERE LastName LIKE 'M%'
but did not complete all
You'll want something like:
select LastName from people where LastName like "M%ae%"
Each % is a wild card for an unknown number of unknown letters.
(Replace people with the name of the relevant table, and the first occurrence of LastName with the information you want from the table)

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%'

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

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')

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.

manipulate string in where clause

I have a variable list_name containing list of names separated by ':'
list_name=(rahul:john:steve) => list_name is of type 'string'
The list contains 1000s of names
Can't change the list_name because we get it from other department/company
want to write a query do to the purpose as shown below, but for that string manipualation would be required i.e. extracting the names from list_names based on the separator ':'
select roll_no from students
where names is rahul or john or steve (problem part)
suggest to use some string manipulation technique in sql to extract the names from the string
PS: I am using proprietary sql and looping is not supported
You can do this in standard'ish SQL using:
select roll_no
from students
where concat(':', list_name, ':') like concat('%:', names, ':%')
In some databases, the where might be written as:
where ':'+ list_name+':' like '%:'+names+':%'
or
where ':'|| list_name||':' like '%:'||names||':%'