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.
Related
I'm new to SQL and I'm stuck on a multiple find in SQL.
Here I'm trying to search table1 in the three columns. But I'm stuch in trying to find two phrases. I'm trying with an OR statement.
If I remove OR LIKE '%paris%' it works but how do I find multiple words/phrases. And would this statement be case sensitive?
I'm also using MySQL to run the above.
SELECT * FROM `table1`
WHERE
CONCAT_WS('|',`target_1`,`target_2`,`target_3`)
LIKE '%london%' OR LIKE '%paris%'
In your code your second condition is sintactically wrong because is missing the a part for the match
so you should repeat the condition as
SELECT *
FROM `table1`
WHERE CONCAT_WS('|',`target_1`,`target_2`,`target_3`) LIKE '%london%'
OR CONCAT_WS('|',`target_1`,`target_2`,`target_3`) LIKE '%paris%'
One option is to use regular expression, then you can also have case insensitive matching (3rd parameter to REGEXP_LIKE)
SELECT *
FROM table1
WHERE REGEXP_LIKE(CONCAT_WS('|',`target_1`,`target_2`,`target_3`), 'london|paris', 'i');
You should repeat the left operand and use (maybe?) multiple conditions i/o concatenating:
SELECT * FROM `table1`
WHERE (`target_1` like '%london%' OR `target_1` like '%paris%')
AND (`target_2` like '%london%' OR `target_2` like '%paris%')
AND (`target_3` like '%london%' OR `target_3` like '%paris%')
I have a table, such as
create table table1(
name varchar(32),
);
And there's some data in it. When I select like this:
select * from table1 where name like 'Jack2%';
there will be Jack2.
But if I select like this:
select * from table1 where name like 'Jack[0-9]%';
there will be nothing;
And I also tried regexp to subsitute like, but it also didn't work!
What's wrong?
You've confused two different pattern-matching mechanisms. SQL LIKE uses % to match anything and _ to match any single character; it does not have anything like [0-9] to match a digit. That looks like a character class from a regular expression.
Standard SQL has no support for regular expressions at all, but MySQL does - you just have to use RLIKE (or REGEXP, but that doesn't read as nicely IMO) instead of LIKE. But that means that you have to replace the % with the regular-expression equivalent .*, too.
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
Fiddle
MySQL REGEX
select * from Table1 where `name` REGEXP 'Jack[0-9]'
You can use RLIKE instead
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
And please note the the '%' operator won't work with RLIKE, you have to use a regular expression pattern like '.*' instead.
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%';
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.
I am trying to select a field based on it meeting one of 3 criteria... and I'm not sure how to do this. I think a RegExp is probably the best method buy I'm unfamiliar with writing them.
Say I have the integer 123, I would like to match the following cases:
123 (thats 123 only with no spaces or other numbers after it)
123-10/12/2007 00:00 (thats 123 with a hyphen and a date, or actually it could be anything after the hyphen)
123_1014859 (thats 123 with an underscore, or again anything after the underscore)
Is there a way to do this using MySQL?
A regex is plausible, but it's not the best performing option. The last comparison put MySQL's regex support as being par with wildcarding the left side of a LIKE statement -- works, but the slowest of every option available.
Based on your example, you could use:
SELECT t.*
FROM YOUR_TABLE t
WHERE t.column LIKE '123-%'
OR t.column LIKE '123_%'
Another alternative, because OR can be a performance issue too, would be to use a UNION:
SELECT a.*
FROM YOUR_TABLE a
WHERE a.column LIKE '123-%'
UNION ALL
SELECT b.*
FROM YOUR_TABLE b
WHERE b.column LIKE '123_%'
UNION ALL will return all results from both tables; UNION removes duplicates, and is slower than UNION ALL for that fact.
select * from foo where bar regexp '^123-|_'
(not tested)
I would avoid using regex inside a SQL statement. Someone can correct me if I am wrong, but MySQL has to use another engine to run the regex.
SELECT * FROM table
WHERE field like "123"
OR field LIKE "123-%"
OR field like "123_%";