Find and show partial text in mysql query - mysql

I have one mysql table that has a column that is "description".
In this column, some descriptions contain a word "peso" (this word can be "Peso", "peso", "PESO" but i need all the results).
What i need is one query that gives me all the rows that contains that word in the description text and show in another column on the result the partial text of the description starting in this word.
How can i do it? I don't have fulltext search.

You would do:
select substring(description, instr(lower(description), 'peso'))) as rest_of_string
from table
where lower(description) like '%peso%';
I'm adding the lower because you might be using a case-sensitive collation.

Related

Boost MySQL query with lot LIKE conditions

Have a table with ~25K rows, the user can provide keywords and negative keywords to filter rows.
It slows down when a user added a lot of keywords and/or negative keywords.
The query looks like this:
SELECT id, title, description
FROM entities
WHERE
(
title LIKE '%keyword_1%' OR description LIKE '%keyword_1%'
OR title LIKE '%keyword_2%' OR description LIKE '%keyword_2%'
OR title LIKE '%keyword_3%' OR description LIKE '%keyword_3%'
)
AND
(
title NOT LIKE '%negative_keyword_1%' OR description NOT LIKE '%negative_keyword_1%'
OR title NOT LIKE '%negative_keyword_2%' OR description NOT LIKE '%negative_keyword_2%'
OR title NOT LIKE '%negative_keyword_3%' OR description NOT LIKE '%negative_keyword_3%'
)
for example, a query with 9 keywords and 130 negative keywords takes ~7 seconds.
Maybe there is a better solution to filter those rows without LIKE? maybe the whole logic is wrong.
Tried MATCH () AGAINST() - it is slower than LIKE for some reason.
For such cases like yours it might help to try to filter the positive matches in database and then test them for a presence of a negative keyword in memory. So try something like:
SELECT id, title, description
FROM entities
WHERE
(
title LIKE '%keyword_1%' OR description LIKE '%keyword_1%'
OR title LIKE '%keyword_2%' OR description LIKE '%keyword_2%'
OR title LIKE '%keyword_3%' OR description LIKE '%keyword_3%'
)
This gives you results which match positive keywords. You can then filter out the matches in memory which contain negative keywords.
Also give an index a chance to be used with LIKE. This reduces your search options from CONTAINS to STARTS WITH but it might be sufficient for your case. Syntax is LIKE 'keyword_1%'.
Fulltext search
Another option is to use MySQL fulltext search by defining a fulltext index on your title and description columns.
CREATE FULLTEXT INDEX idx_title ON entities(title);
CREATE FULLTEXT INDEX idx_description ON entities(description);
Or you can merge these two columns into a single column - for the search purposes. Then you need only one fulltext index.
Query syntax is then following:
MATCH (title) AGAINST ('keyword_1')
instead of
title LIKE '%keyword_1%'
For this search I would also recommend to filter positive matches only in a database and then filter out the matches in memory which contain negative keywords.

Searching table efficiently for specific phrase

I want to search an entire column for a specific phrase. I know I can use the SQL statement of SELECT description FROM questions WHERE description LIKE '%what%' AND description LIKE '%if%' to search for the phrase "What if" in the description column. My problem is that if I have a million entries, then searching through the column might take a while.
Is there a way to search through an entire column efficiently to check if a specific phrase exists?
Full text search provides exactly what you want. By creating a text index for your search column, mysql will look for your phrase in that column efficiently, and will return to you a score based match for your phrase, which you can effectively use to get your result.

To find a column name in MS SQL

I have to find the column name which has a particular value. For example : I have to find all the column names which has a value 'Availability' stored in that particular column.
To find column name that contain availabvility you could use INFOMATION_SCHEMA.COLUMNS:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%Availability%';
or sys.columns
SELECT *
FROM sys.columns
WHERE Name LIKE '%Availability%';
EDIT:
To find specific value in all tables you could use ApexSQL Search Plugin
Text search
Search for text, numbers, dates and unique identifiers in your tables
and views
Image from: http://www.apexsql.com/sql_tools_search.aspx
I don't think so, you have any shortcut to find the value level check. You may need go and check the respective tables and use pattern matching(i.e. like '%availability%') to find the value.
My shortcut would be: You should have general idea about you possible column names which can contain the expected availability value. Then use lad2025 data dictionary queries to find the relevant tables and the column names. Then you can go ahead and run a separate select query on each table/ column.

How to search a word in MySQL table? MATCH AGAINST or LIKE?

I have a table for some companies that may have many branches in different countries. These countries are inserted in countries field.
Now, I have to make a searching system that allows users to find companies that have any branch in a specific country.
My question is: Which one do I have to use ? MATCH AGAINST or LIKE ? The query must search all records to find complete matched items.
attention: Records may have different country name separated with a comma.
MATCH AGAINST clause is used in Full Text Search.
for this you need to create a full text index on search column countries.
full text index search is much faster than LIKE '%country%' serach.
I would change the implementation: having a field that contains multiple values is a bad idea, for example, it's difficult to maintain - how will you implement remove a country from a company ?.
I believe that a better approach would be to have a separate table companies_countries which will have two columns: company_id and country_id, and could have multiple lines per company.
You should use LIKE . Because as #Omesh mentioned MATCH AGAINST clause is used for Full Text Search.. And Full Text Search need entire column for search.

mysql - extract specific words from text field using full text search

My question is a little simillar to Extract specific words from text field in mysql, but now the same.
I have a text field with words inside. In my language word can have many different endings. I need to find this endings.
I use fulltext search of mysql, but I would need to have access to the index database where all the field is "cut" to words and words are counted. I could then search for "test*" and I could quickly find "test", "tested", "testing". I need the list of all endigns that exist in my database, that is my primary goal.
As it is I can get the records with specific "test*" words in it, but I need not only to locate the occurence in the field, but to group somehow so I get the list of all the words that for example start with "test". I don't need location in which record they are, just a list, grouped so that "testing" is not written 10 times but only once (maybe a counter of how many times it is found but not necessary).
Is there a way to extract this info from fulltextsearch field or should I explode all this fields to words and make a index table full of words and just do a "like "word%" and group by the different results? I am not sure how to do that either in practice, but just to point me to the right direction please.
So to summarize: I have a text fied and I need to find out which words are inside that start with "test", like "tested", "test", "testing" etc... It doesn't make sense in English but in my language it does as we have same word on different endigns and there are so many of them, somethimes 20, I need to find out which ones are there so I can make a synonims table ;-)
UPDATE:
Database has columns ID (int), ingredients (text) and recipe (text).
Data in ingredients are cooking ingredients with different endings like:
1 egg
2 eggs
etc.
You can dump all words that are present in an index. And that would also show frequency of each word. E.g. test is used 200 times and testing is used 300 times.
Manual for that: http://dev.mysql.com/doc/refman/5.0/en/myisam-ftdump.html