I have a database with 150,000 records and I need to match its FULL column value or records, with some parts of the string.
**
As i want to check if the STRING contains the COLUMN records and NOT!
if the COLUMN contains the string
**
(Example below)
For testing purposes
Lets say the database has a TABLE , 1 COLUMN and 1 record as the records are similar to this:
come to me
and i need to match it with this #STRING
She wants to come to me now
I want to execute something similar to :(but this doesn't work of course)
SELECT * from TABLE where #STRING like '%'+COLUMN+'%'
I can't seem to solve this with SQL the usage of PHP is possible but prefer if the solution is with SQL but if the solution with PHP is available please propose it and note that the database has over 150,000 records
SELECT * from TABLE where LOCATE(COLUMN, #STRING)>0;
LOCATE(a,b) returns a number giving the character location of a in b, or returns 0.
See Mysql LOCATE()
The docs discuss that LOCATE() is only case sensitive when one of the strings is a 'binary string'. That probably doesn't affect your use case, though if it became an issue you could CONVERT() the binary strings to a locale and use LOWER() to get lower case.
MySQL String Functions
The dynamic like syntax for mysql is
SELECT * from TABLE where #STRING like CONCAT('%',COLUMN,'%')
Related
I have a question about aproach for searching strings longer then the one stored in DB.
For example: String stored in DB is "somestring". I need to mach it with changed string e.g. "somestringXY".
I have a DB with stored models but in the files I need to parse, strings are often modified.
What I can do is:
I can SELECT * 'string' from Table and then search needle via foreach and substr/preg_match.
I can also SELECT * 'string' FROM Table to array and then search via the in_array function.
I can also make a loop, cut last letter, make a select on DB and find match. If no match do loop again.
Let's say that there shouldn't be double matches so in DB there will not be strings (somestringXY, somestringX, somestring).
Maybe there are also other approaches? Of course Like in SQL is not helpful because searched string is longer then stored in DB.
I don't know if there exists a specific MySQL way of doing this, but I would go with this route:
$substrarray = array("'somestri'","'somestrin'", "'somestring'","'somestringX'","'somestringXY'");
$substrings = implode(",",$substrarray);
$query = "SELECT * FROM table WHERE string IN (".$substrings.")";
The problem of course is that this might bring you unwanted results, depending what you are looking for and how similar those strings are to eachother. But the pro is that it only does one call, instead of multiple when you check after every time you have deleted a letter.
Suppose I have a user table. One of a column of the table store for user first name.
Also suppose there are there rows in the table. The user first names are as follows :
'Suman','Sumon','Papiya'.
Now I want a mysql query if an user search from the table by user first name with 'Suman' then the result will shows two rows one for 'Suman' and another for 'Sumon'.
You can use soundex it will compare if the sound of values in firstname matches to the sound of provided word
According to docs
When using SOUNDEX(), you should be aware of the following
limitations:
This function, as currently implemented, is intended to work well with strings that are in the English language only. Strings in other
languages may not produce reliable results.
This function is not guaranteed to provide consistent results with strings that use multi-byte character sets, including utf-8.
select *
from t
where soundex(firstname)=soundex('Suman')
Demo
I wanted to compare two strings in MySQL irrespective of the order of two strings:
Here is an example, Say i have a string as 'A1,B1,C1' and i wanted to find out how many rows are there in table where the column value contains the same string. This can be done easily with like query as given below:
select count(1) as attr_count from attribute_lists where attr_tab like :value_names;
I will execute this query from PHP using PDO and the string 'A1,B1,C1' will be binded to value_names. However what i also want is if any row contains the same set values but in different order then also they should be considered in count. Say if there is a row with column value as 'B1,A1,C1' then that should be matched and counted as 1.
Irrespective of the order in which the strings are they should be matched. Any help on how can i write such a query?
select count(1) as attr_count from attribute_lists where attr_tab like '%'+value_name'%'
I'm looking for a way to compare database values against a given query using MySql
(in oppose of searching the query in the db)
I will elaborate:
I have a table that holds comma separated keywords and a result for each block of keywords
for example :
col 1 col 2
Mercedes,BMW,Subaru car
Marlboro,Winston cigarette
today im taking the user query (for example - Marlboroligt)
as you can see if i will search for the value 'Marlboroligt' in the db i will get no results.
for that matter i want to search 'Marlboro' from the db inside the query and return 'cigarette'
Hope my explanation is sufficient and that this is actually possible :-).
Normalization will help you the most. Otherwise, search in comma delimited fields is discussed here:
Select all where field contains string separated by comma
...and to search 'Marlboroligt' instead of 'Marlboro Light', you can try looking into the LEVENSHTEIN function, or maybe the Soundex encoding (which looks like too little bang for too large a buck, but then again, maybe...).
I see the following possible solutions:
setup a keyword-search engine like Sphinx and use it to search keywords in your db
normalize your db - col1 must contain the only keyword
use like patterns
select col2 from mytable where col1 like "%Marlboro%"
like slows down your application and can have substring-related issues.
I am wondering if i can put something in my query which searches for a word within a string. For example, if i have a field named user_purchased, i want to query it and pull only records where the word 'dog' exists within the string.
I know i can do this using php like this
(!stristr($row['user_purchased'], 'dog'))
BUT, i'm wondering if i can do this in the query itself instead and that this will perhaps speed up performance.
Thanks in advance for help on this
You can use LIKE:
WHERE your_col LIKE '%dog%'
If you want better performance you could look at a full text search. This uses a FULLTEXT index to quickly find the matching rows.
In MySql, you can use INSTR(str,substr)
Quote from the MySql manual at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr
INSTR(str,substr)
Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed.
mysql> SELECT INSTR('foobarbar', 'bar');
-> 4
mysql> SELECT INSTR('xbar', 'foobar');
-> 0
This function is multi-byte safe, and is case sensitive only if at least one argument is a binary string.
SELECT * FROM table WHERE user_purchased LIKE '%dog%';