ll've looked into the like function, as well as other solutions but I think what I'm trying to do can be accomplished with one query. Here is what I have so far.
"SELECT name FROM tblname where name like '%jons%'"
i excepted answer fetch the name "jon" how can i do. please help me
SELECT `name`
FROM `tblname`
WHERE 'jons' LIKE CONCAT('%', `name`, '%');
Also you can use two LIKE methods with OR operator.
SELECT name
FROM tblname
WHERE name like 'jon%';
Related
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'%
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%'
i need to find multi string in multi columns in mysql.
select * from myTable WHERE name LIKE '%stack%';
This obviously works. However, what I need to do is that if both name and surname are checked multi string would do something like this:
select * from myTable WHERE (name or surname) LIKE '%stack%','%over%','%flow%';
if similar above code that work does not exist. please tell me how can checking multi string in a column in mySQL with like statement:
select * from myTable WHERE name LIKE '%stack%','%over%','%flow%';
You can keep it simple and do:
select * from myTable WHERE name LIKE '%stack%' OR
name LIKE '%over%' OR
name LIKE '%flow%';
or:
select * from myTable WHERE name LIKE '%stack%' AND
name LIKE '%over%' AND
name LIKE '%flow%';
This doesn't look as nice as the REGEXP from Young, but it doesn't have the unexpected behaviour REGEXP can have given that user input can be anything. With REGEXP you really need to know what you're doing.
You can try:
select * from myTable WHERE name REGEXP 'stack|over|flow';
Though it's not a like solution.
I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.
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.