mysql search 3 columns with dynamic count of words - mysql

I have a table with 3 fields. These fields are: name, surname, address. When I search by one word and any of fields contains the word, this row should be selected. When I search by 2 words 2 of 3 fields should have these words and so on. I have ready query in my hand.
This is for 2 word search string:
where (name like %word1% or surname like %word1% or address like %word1%)
and (name like %word2% or surname like %word2% or address like %word2%)
Number of intermediate ands = words - 1. Where part is generated depending on number of words in search string. How can I optimize this query?

Related

Match exact pattern [duplicate]

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 1 year ago.
I have a MySQL table that contains books information. Part of that information is a special character (^) separated list of values that corresponds to the book names. I'm having some problems getting the correct information out of the database. Table looks like
id OwnedBooks CustomerID
1 Harry^Two States^Tintin 101
2 Harry Potter^Tintin 290
3 Harry Prizoner of Azhaban 278
So when I search for 'Harry' in the ownedbooks column, I should get only one record i.e. (record id=1)
My query looks like below
SELECT * FROM books where ownedbooks like '%Harry%'
This query return all the records as I have used like, but I wanted to match the exact string with (^) as a separation.
When I search for 'Harry Potter' it should return the second record i.e (record id=2)
If you want to match a book named exactly "Harry" then you may use the following LIKE logic:
SELECT *
FROM books
WHERE ownedbooks LIKE '%^Harry^%' OR -- appears in the middle of the list
ownedbooks LIKE 'Harry^%' OR -- appears in the beginning of the list
ownedbooks LIKE '^Harry'; -- appears in the end of the list
You would be making a good design decision to move away from storing ^ delimited data in your table. Instead, get each book title onto a separate record.

mysql select query (dictionary search)

I have a table and I want to get the row with the value that most closely resembles the searched text. For example if searched text is 'rakul' it should be able to select row with value 'rahul'
Just change the Customers value to your table name and the FirstName to what column you want to search and the David to your word want to search for it
SELECT * FROM Customers WHERE FirstName LIKE '%David%';

Extracting 2-letter capital substrings from table column in sql

I have an SQL table on users where particular accounts are tagged with country code (2 letter words in uppercase) while other substrings in the tags (all separated by commas) are either in lowercase or more than 2 letters long.
In user table
Eg:
id User_tags
1 alu,US,ATD
2 GB,xx
3 ol,tuds,FR
Users 1,2 and 3 are tagged to countries US, GB and FR and I need to extract them from the user_tags column. I understand that regex functions are needed but I am not able to make them work in an SQL query.
Create a country code ref table and join to using the below. I don’t have sqlserver open, so unable to double check syntax , but it should work.
Select *
From yourtable y left join refcountry r
On charindex (r.code+’,’,y.string+’,’)>0
Note this might be slow for a large dataset
If not sqlserver find the equiv function for charindex in your rdbms

Sphinx search query - condition for numbers in varchar column

I have items and list in which categories exists:
id | name | categories(varchar)
1 | bike red | 2,5,18
2 | bike black | 4,7,13
With Sphinx I need to serach for example: bike AND only from category 5
Is any good way how search in column categories?
In MySql I could write: WHERE name LIKE '%bike%' AND categories LIKE '%5%'
But my Sphinx index is big and searching could be not efective. Is any way like create integer ENUM list or? What could be good solution?
Thanks
Sphinx has Multi-Value Attributes http://sphinxsearch.com/docs/current.html#mva . pretty much perfect for this!
It works kinda like a numeric set in MySQL! (you have multiple categories, so set, not enum)
It will even automatically parse a string list of numbers seperated by commas during indexing.
sql_query = SELECT id,name,categories FROM item
sql_attr_multi = uint categories from field;
Then a sphinxQL query...
SELECT * FROM item WHERE MATCH('bike') AND categories=5
(This may look confusing if familar with MySQL. an equality filter on a MVA attribute, actully just means equals one of the values. If want could write categories IN (5) - same effect)

Find all users having specific number / string in a sql column

I've a db table named "users" which has a column named "alerts". The alert column has the comma separated data in following sequence;
UserA -> 2314|1,2315|1,2316|3
UserB -> 4235|2,2315|1,2314|3
UserC -> 342|5,2314|1,2316|3
Where if I split the comma and then slash separated value then;
2314 = pid
1 = uid
If I want to search all users having specific pid e.g. 2314 and to list them OR fetch them all, how and what SQL query I should have to use?
On the face of it, you would search for ',2314|', but the problem is that numbers at the start of the value don't have a preceding comma.
There are 3 ways to solve it.
Handle the start case and middle case separately:
select * from users
where alerts like '2314|%'
or alerts like '%,2314|%'
Combine the cases by adding a comma to the beginning of the value to make it look like a middle case:
select * from users
where concat(',', alerts) like '%,2314|%'
Use a regular expression to combine the cases:
select * from users
where alerts rlike '(^|,)2314\|'
It would be better to redesign your tables to break out a new table to handle the many-to-many relationship you have shoehorned into one column.