MySQL Regexp search keyword - mysql

I use this query
SELECT keyword
FROM files
UNION SELECT keyword
FROM search
WHERE keyword
REGEXP "/(honda)|(jazz)|(manual)/"
AND keyword != "honda jazz manual"
ORDER BY keyword ASC
LIMIT 0 , 10
but I got this result
Big bang theory reference
I want to asking you guys, how to use regexp to search keyword.

Please try the following:
SELECT keyword
FROM
(SELECT keyword
FROM files
UNION SELECT keyword
FROM search) allkeywords
WHERE keyword REGEXP '(honda|jazz|manual)'
AND keyword != 'honda jazz manual'
ORDER BY keyword ASC
LIMIT 0 , 10
See http://sqlfiddle.com/#!2/9341ff/5
Explanation:
(1) The unioned query needed making into a subquery to allow the WHERE clause to affect all of it.
(2) The REGEXP syntax was slightly wrong - parentheses round the whole OR'd expression, not individual items.

Related

MySQL: Limit the number of characters in LIKE clause?

I'm using this query in my autocomplete feature:
SELECT description FROM my_table WHERE LIKE "%keyword%"
But this query returns the entire content of the field which is sometimes too long.
Is it possible to limit the number of characters before and after "keyword" ?
I suggest using MySQL's REGEXP operator here. For example, to accept a maximum of 10 characters before and after keyword, you could use:
SELECT description
FROM my_table
WHERE col REGEXP '^.{0,10}keyword.{0,10}$';
Note that if you intend to match keyword as a standalone word, you may want to surround it by word boundaries in the regex pattern:
SELECT description
FROM my_table
WHERE col REGEXP '^.{0,10}\\bkeyword\\b.{0,10}$';
To show for example 5 characters before and after you word you can do it using RIGHT, LEFT and SUBSTRING_INDEX
select description, concat(RIGHT(SUBSTRING_INDEX(description, 'keyword', 1),5), 'keyword', LEFT(SUBSTRING_INDEX(description, 'keyword', -1),5) ) as snippet
from my_table
where description like "%keyword%";
Check it here : https://dbfiddle.uk/MZcVJgEL

How to use REGEXP in mysql for matching words from a text

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.

How to use the result of GROUP_CONCAT in an RLIKE query?

Given a table keywords which contains some keywords for searching, and another table titles of which title to search. I tried GROUP_CONCAT all words in keyword and fed the result ('w1|w2|w3|w4') in a RLIKE query as following:
select title from titles where title rlike
(select group_concat(distinct word separator '|') from keywords) as keyword;
But the statement violated the SQL syntax. How could I fix the statement (assuming full-text search is unavailable)?
That seems like a very odd thing to do. Why not just use exists?
select t.title
from titles t
where exists (select 1
from keywords kw
where t.title rlike kw.word
);

SELECT Mysql with combination of two where

I want select 2 Rows from the mysql table with additional "LIKE" and "AND" clauses..
Wit like clause I want to find only word starting with "a%"..
But I can't find the syntax error. Can you give me some hints.??
SELECT word,description
FROM word
WHERE(`language` = CONVERT( _utf8 'Tedi' USING armscii8 ) AND like 'a%') AND `visible` =1
many thanks in advance.
Regards,
Koko
The syntax error is a missing expression before the LIKE comparison operator.
We'll have to guess what expression you wanted to do the comparison operation on, so I'll just choose the first column from the SELECT list, to demonstrate:
SELECT w.word
, w.description
FROM word w
WHERE w.language = CONVERT( _utf8 'Tedi' USING armscii8 )
AND w.word LIKE 'a%'
AND w.visible = 1
The predicates in the WHERE clause specify the criteria that a row has to satisfy before it will be returned, it doesn't care whether that's zero rows, two rows or a brazilian rows.

Best way to return "champion" that exists in a table in case the given query "championship" is not found

I have a very big table with strings.
Field "words":
- dog
- champion
- cat
- this is a cat
- pool
- champ
- boots
...
In my example, if a select query is looking for the given string "championship", it won't find it because this string is not in the table.
In that case, I want the query to return "champion" from the table, i.e. the longest string in the table that begins the given word "championship".
The possible match (if found) is the longest one in table between championship, or championshi, or championsh, or champions, ..., or cham, or cha, or ch, or C.
Question: I want to return longest string in table that starts a given string.
I need high speed. Is there a way to create index and query in order to have fast execution of queries?
Here's one query that will return the specified result:
SELECT t.mycol
FROM mytable t
WHERE 'championship' LIKE CONCAT(t.mycol,'%')
ORDER
BY LENGTH(t.mycol) DESC
LIMIT 1
This query can't do a index range scan, it's going to have to be full scan, but it may be able to use an index to satisfy the query.
If you can restrict the search to a finite number of leading letters that need to match to be considered a "hit", you could include another predicate. For example, to match at least 4 characters:
SELECT t.mycol
FROM mytable t
WHERE 'championship' LIKE CONCAT(t.mycol,'%')
AND t.mycol LIKE 'cham%'
ORDER
BY LENGTH(t.mycol) DESC
LIMIT 1
--or--
AND t.mycol >= 'cham'
AND t.mycol < 'chan'
You are a little vague with 'the longest string in the table that begins the given word "championship".' Would "championing" count as a match?
Perhaps the following will help. If you have an index on words, then the following will return the last word before the given word. It should maximize the initial sequence of matches:
select word
from t
where words <= 'championship'
order by words desc
limit 1;
This isn't exactly what you are asking for, but it might work in practice.
EDIT:
If you are looking for an exact match, then the following should use an index on words effectively and return what you want:
select word
from t
where word in ('championship', 'championshi', 'championsh', 'champions', 'champion',
'champio', 'champi', 'champ', 'cham', 'cha', 'ch', 'c')
order by word desc
limit 1;
It is a bit brute force, but it should have the property of using the index to speed up the query.
Have a look at this article:
http://blog.fatalmind.com/2010/09/29/finding-the-best-match-with-a-top-n-query/
It explains the solution from this SO question:
How to use index efficienty in mysql query
The solution pattern looks like this:
select words
from (
select words
from yourtable
where words <= 'championship'
order by words desc
limit 1
) tmp
where 'championship' like concat (words, '%')