Hi I am writing my own MySQL query where I need a result of records as follows.
Word in a table - ABC XYZ
My string - ABC XYZQWER
when I ran my query as below -
SELECT * FROM myTABLE where `column` LIKE 'ABC XYZQWER%';
I am getting empty result. I am aware of the fact that MySQL LIKE matches the result of string.
I need a way to figure this out.
I I searched it using 'ABC X' - it is giving me a proper result.
You can use the function LOCATE():
SELECT `column`
FROM myTable
WHERE LOCATE(`column`, 'ABC XYZQWER') = 1;
As long as there is a value ABC XYZ in the column named column, the result of the query will be at least:
+---------+
| column |
+---------+
| ABC XYZ |
+---------+
Finding an inner match
Finding a matching string like 'BC', which is inside the search string 'ABC XYZQWER', is possible by using the compare operator >=. So the WHERE clause will look like this:
WHERE LOCATE(`column`, 'ABC XYZQWER') >= 1;
It is because you dont have a work which has QWER. You are actually searching for a word which is not present. So you are getting a zero result.
For eg:
Word : qwertyuiuioo
search String : qwerty
select * from table where word like qwerty% you will get the result.
% takes any number of characters after the letters you have given which is not matching any value in the table.
Try this:
SELECT * FROM myTABLE a WHERE 'ABC XYZQWER' LIKE CONCAT(a.column, '%');
Here are some examples of how one might use LIKE clause in SQL queries:
SELECT * FROM myTABLE where column LIKE 'ABC%';// matches ABCD, ABC D but not DABC
SELECT * FROM myTABLE where column LIKE '%ABC%';// matches any string that contains ABC anywhere in the string eg. DABC, D ABC but not D AB C
for your case you would do something like this:
SELECT * FROM myTABLE where column LIKE 'ABC XYZ%';
You won't be able to do perfect substring searches although you can apply Levenshtein distance searches as described here (Levenshtein Distance MySQL Function). But do note these work a bit differently from LIKE clause in a way that it gives you the result based on the distance you specify for a search.
And after that you can use it like this:
SELECT * FROM mytable WHERE levenshtein("ABC XYZQWER",column) <= 4
This will give you the result set you are looking for; it will also give other words which fall under this range.
Related
In my column I can have either a string like : "data+" or "data+data+data+..(undefined times)..+"
I simply need to get the column where I have multiples data and not only one.
I tried with
mycol NOT LIKE '%+'
But it didn't work...
Actually I don't know the data it is a string that varies : 'blabla' or 'aString' or 'whatever'
If I had in my columns
'jdsjpgsg+jdsjpgsg+', 'dvgff+', 'ffef+eefds+ghghgh+'
I want to select only
'jdsjpgsg+jdsjpgsg+',
'ffef+eefds+ghghgh+',
NOT 'dvgff+' !
if you want to search '..xxx+..' then you should be use xxx+%
if you want to search '..+xxx..' then you should be use %+xxx
if you want to search '..++..' then you should be use %++%
if you want to search '..+..+..' then you should be use %+%+%
It is what I get too and I dont want that. It is actually what i don't want to select. If I had jdsjpgsg+jdsjpgsg+ in my table I want to select it and NOT jdsjpgsg+ It is tricky...
so you can try like '%+%+%' to exclude just one '+'
CREATE TABLE TestTable
(`text` varchar(90))
;
INSERT INTO TestTable
(`text`)
VALUES
('jdsjpgsg+jdsjpgsg+'),
('dvgff+'),
('ffef+eefds+ghghgh+')
;
select * from TestTable
where text like '%+%+%'
| text |
|--------------------|
| jdsjpgsg+jdsjpgsg+ |
| ffef+eefds+ghghgh+ |
SQL Fiddle Demo Link
The % is the wildcard character. You should be using the % after data. Try like:
SELECT * FROM `table` WHERE `mycol` NOT LIKE 'data+%'
The above query will filter out all the records that have any characters after data+.
I have a table in mysql with column called type. contents are below
Type
Test 12
Test abc
start 1
start abcd
end 123
Now I want to select records where type starts with Test
Expected result:
Test 12
Test abc
But I am getting either
Test 12
or empty results
I have tried like below:
select * from table where type = 'Test 12'
select * from table where type = '%Test%'
select * from table where type = '%Test'
What should be the correct sql statement.
If you want to do partial matches you need the LIKE operator:
SELECT * FROM table WHERE type LIKE 'Test%'
Use the Like keyword
select * from table where type LIKE 'Test%'
The equality (=) operator doesn't accept wild cards. You should use the like operator instead:
SELECT * FROM table WHERE type LIKE 'Test%'
Sooo close! You want to start with the string, then don't use % in the start, and use LIKE instead of =
select * from table where type LIKE 'Test%'
I have a mysql query like :
SELECT name FROM table_name WHERE name LIKE '%custom%' limit 10
It retruns me 2 rows from my custom table.
I want to get records which contains either of any word from the text c cu cus cust usto stom tom om m also.
I tried below query :
SELECT name FROM table_name WHERE name like '%custom%' OR name REGEXP 'c|cu|cus|cust|usto|stom|tom|om|m' limit 10
Above query returning me 7 records but these 7 records does not have such 2 records which 1st query result have.
How to get that? Or any other way to get these result in mysql?
EDIT : Here I also want to order by maximum substrings matches in second query.
Try this:
SELECT name FROM table_name WHERE name REGEXP 'custom' limit 10;
There is no need of LIKE with REGEXP, but REGEXP are slower then LIKE. So if your table have so many records then REGEXP quesries are slower.
Try this:
SELECT name FROM table_name WHERE name REGEXP 'custom|c|cu|cus|cust|usto|stom|tom|om|m' limit 10
What we did above is that we combined custom with the rest of the patterns, and we made them all use REGEXP.
You need to add word boundaries, which in MySQL are [[:<:]] for start of word and [[:>:]] for end of word:
SELECT name
FROM table_name
WHERE name REGEXP '[[:<:]](c|cu|cus|cust|usto|stom|tom|om|m)[[:>:]]'
limit 10
See live demo.
Note the brackets around the alternation.
Compare comma-separated string in MySQL column where column is also comma separated
For Example:
Id name catid
1 abc 4,5,2
2 bcd 5
3 efg 9,1,7
SELECT * FROM TABLE WHERE catid IN (2,5,6)
Here 2,5,6 I have to compare with the catid value to get the result.
Any Help to get the right out put, I used FIND_IN_SET, but could not make it work for my case
according to your problem you can use LIKE operator instead of IN
SELECT * FROM testchintan WHERE CONCAT(',',catid,',') LIKE '%,5,%'
OR CONCAT(',',catid,',') LIKE '%,2,%' OR CONCAT(',',catid,',') LIKE '%,6,%'
OR YOU can use REGEXP
SELECT *
FROM testchintan
WHERE catid REGEXP '[4,9,5]'
OR
SELECT *
FROM testchintan
WHERE
REPLACE(catid,',','|') REGEXP '[9,7]'
Note that I'm not seriously advocating this as a solution...
SELECT * FROM my_table WHERE FIND_IN_SET(5,catid) OR FIND_IN_SET(2,catid);
What's the difference between
SELECT foo FROM bar WHERE foobar='$foo'
AND
SELECT foo FROM bar WHERE foobar LIKE'$foo'
= in SQL does exact matching.
LIKE does wildcard matching, using '%' as the multi-character match symbol and '_' as the single-character match symbol. '\' is the default escape character.
foobar = '$foo' and foobar LIKE '$foo' will behave the same, because neither string contains a wildcard.
foobar LIKE '%foo' will match anything ending in 'foo'.
LIKE also has an ESCAPE clause so you can set an escape character. This will let you match literal '%' or '_' within the string. You can also do NOT LIKE.
The MySQL site has documentation on the LIKE operator. The syntax is
expression [NOT] LIKE pattern [ESCAPE 'escape']
LIKE can do wildcard matching:
SELECT foo FROM bar WHERE foobar LIKE "Foo%"
If you don't need pattern matching, then use = instead of LIKE. It's faster and more secure. (You are using parameterized queries, right?)
Please bear in mind as well that MySQL will do castings dependent upon the situation: LIKE will perform string cast, whereas = will perform int cast. Considering the situation of:
(int) (vchar2)
id field1 field2
1 1 1
2 1 1,2
SELECT *
FROM test AS a
LEFT JOIN test AS b ON a.field1 LIKE b.field2
will produce
id field1 field2 id field1 field2
1 1 1 1 1 1
2 1 1,2 1 1 1
whereas
SELECT *
FROM test AS a
LEFT JOIN test AS b ON a.field1 = b.field2
will produce
id field1 field2 id field1 field2
1 1 1 1 1 1
1 1 1 2 1 1,2
2 1 1,2 1 1 1
2 1 1,2 2 1 1,2
According to the MYSQL Reference page, trailing spaces are significant in LIKE but not =, and you can use wildcards, % for any characters, and _ for exactly one character.
I think in term of speed = is faster than LIKE. As stated, = does an exact match and LIKE can use a wildcard if needed.
I always use = sign whenever I know the values of something. For example
select * from state where state='PA'
Then for likes I use things like:
select * from person where first_name like 'blah%' and last_name like 'blah%'
If you use Oracle Developers Tool, you can test it with Explain to determine the impact on the database.
The end result will be the same, but the query engine uses different logic to get to the answer. Generally, LIKE queries burn more cycles than "=" queries. But when no wildcard character is supplied, I'm not certain how the optimizer may treat that.
With the example in your question there is no difference.
But, like Jesse said you can do wildcard matching
SELECT foo FROM bar WHERE foobar LIKE "Foo%"
SELECT foo FROM bar WHERE foobar NOT LIKE "%Foo%"
More info:
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
A little bit og google doesn't hurt...
A WHERE clause with equal sign (=) works fine if we want to do an exact match. But there may be a requirement where we want to filter out all the results where 'foobar' should contain "foo". This can be handled using SQL LIKE clause alongwith WHERE clause.
If SQL LIKE clause is used along with % characters then it will work like a wildcard.
SELECT foo FROM bar WHERE foobar LIKE'$foo%'
Without a % character LIKE clause is very similar to equal sign alongwith WHERE clause.
In your example, they are semantically equal and should return the same output.
However, LIKE will give you the ability of pattern matching with wildcards.
You should also note that = might give you a performance boost on some systems, so if you are for instance, searching for an exakt number, = would be the prefered method.
Looks very much like taken out from a PHP script. The intention was to pattern-match the contents of variable $foo against the foo database field, but I bet it was supposed to be written in double quotes, so the contents of $foo would be fed into the query.
As you put it, there is NO difference.
It could potentially be slower but I bet MySQL realises there are no wildcard characters in the search string, so it will not do LIKE patter-matching after all, so really, no difference.
In my case I find Like being faster than =
Like fetched a number of rows in 0.203 secs the first time then 0.140 secs
= returns fetched the same rows in 0.156 secs constantly
Take your choice
I found an important difference between LIKE and equal sign = !
Example: I have a table with a field "ID" (type: int(20) ) and a record that contains the value "123456789"
If I do:
SELECT ID FROM example WHERE ID = '123456789-100'
Record with ID = '123456789' is found (is an incorrect result)
If I do:
SELECT ID FROM example WHERE ID LIKE '123456789-100'
No record is found (this is correct)
So, at least for INTEGER-fields it seems an important difference...