Matching content in two different columns-MySQL - mysql

Let's say I have a list of person names and a list of social media URL's (that might or might not contain a portion of the person names).
I'm trying to see if the full name is not contained in the list of URL's I have. I don't think a "not like" would work here (because the URL has plenty of other characters to throw back a result), but I can't think of any other way to address this. Any tips? The closest I could find was from this:
Matching partial words in two different columns
But I'm unsure if that applies here.

Just use SELECT * FROM yourtable WHERE url LIKE '%name%' % means any characters even whitespace. Then just check if it returned any rows.
From mysql doc:
% matches any number of characters, even zero characters.
mysql> SELECT 'David!' LIKE 'David_';
-> 1
mysql> SELECT 'David!' LIKE '%D%v%';
-> 1
So let's say these are your url's in your list:
website.com/peterjohnson
website.com/jackmiller
website.com/robertjenkins
Then if you would do:
SELECT * FROM urls WHERE url LIKE '%peter%'
It would return 1 row.
You can also use NOT LIKE so you will get all the rows not containing the name.

Related

How do I create a SELECT conditional in MySQL where the conditional is the character length of the LIKE match?

I am working on a search function, where the matches are weighted based on certain conditions. One of the conditions I want to add weight to is matches where the character length of the query string in a LIKE match is longer than 4.
This is what I want to the query to look like, roughly. %s is meant to represent the actual match found by LIKE, but I don't think it does. I'm wondering if there is a special variable in MySQL that does represent the precise character match found by LIKE.
SELECT help.*,
IF(CHAR_LENGTH(%s) > 4, 2, 0) w
FROM help
WHERE (
(title LIKE '%this%' OR title LIKE '%testy%' OR title LIKE '%test%') OR
(content LIKE '%this%' OR content LIKE '%testy%' OR content LIKE '%test%')
) LIMIT 1000
edit: I could in the PHP split the search string array into two arrays based on the character length of the elements, with two separate queries that return different values for 'w', then combine the results, but I'd rather not do that, as it seems to me that would be awkward, messy, and slow.
Check out FULLTEXT as another way to discover rows. It will be faster, but won't address your question.
This probably has the effect you want.
SELECT ....
IF ( (title LIKE '%testy%' OR
content LIKE '%testy%'), 2, 0)
....
Note that the "match" in your LIKEs includes the %, so it is the entire length of the string. I don't think that is what you wanted.
REGEXP "(this|testy|that)" will match either 4 or 5 characters (in this example). It may be possible to do something with REGEXP_REPLACE to replace that with the empty string, then see how much it shrank.
I think the answer to my question is that what I wanted to do isn't possible. There is no special variable in MySQL representing the core character match in a WHERE condtional where LIKE is the operator. The match is the contents of the returned data row.
What I did to reach my objective was took the original dynamic list of search tokens, iterated through that list, and performed a search on each token, with the SQL tailored to the conditions that matched each token.
As I did this I built an array of the search results, using the id for the database row as the index for the array. This allowed me to perform calculations with the array elements, while avoiding duplicates.
I'm not posting the PHP code because the original question was about the SQL.

mysql MATCH AGAINST weird characters query

I have a table where the field "company_name" has weird characters, like "à","ö","¬","©","¬","†", etc. I want to return all "company_name"s that contain these characters anywhere within the string. My current query looks like this:
SELECT * FROM table WHERE
MATCH (company_name) AGAINST ('"Ä","à","ö","¬","©","¬","†"' in natural language mode);
But I keep getting no data from the query. I know this can't be the case, as there are definitely examples of them I can find manually. To be clear, the query itself isn't throwing any errors, just not returning any data.
The minimun word length is 3 pr 4 .
you can change it see manial
https://dev.mysql.com/doc/refman/8.0/en/fulltext-fine-tuning.html
or use regular expressiions
SELECT * FROM table WHERE
ompany_name REGEXP '[Äàö¬©¬†]+';
SELECT *
FROM table
WHERE company_name LIKE '%[^0-9a-zA-Z !"#$%&''()*+,\-./:;<=>?#\[\^_`{|}~\]\\]%' ESCAPE '\'
This will find any wacky stuff, including wide characters or 'euro-ASCII' or emoji.

sphinx search return match and surrounding characters

In Sphinx (using mysql connection), how can I match for a term and also get, let's say 5 characters before and after the match?
For example: a row contains this is a word in a sentence.
When I query: SELECT term FROM table WHERE MATCH('"word*"')
I would want to see is a word in a s returned. Is this possible?
Edit Using #barryhunter's helpful answer below, now trying to figure out how to fit this into my query:
SELECT field1,field2,SPHINX_SNIPPETS(field3,indexName, "word") as snippet FROM tableName
Thats what CALL SNIPPETS is designed for. Although its counted in words, not charactors.
http://sphinxsearch.com/docs/current/sphinxql-call-snippets.html
CALL SNIPPETS('this is a word in a sentance','index1','word', 2 AS around, 5 as limit_words);
would get back
... is a word in a ...
add '' as chunk_separator is dont want the ellipsis
Edit To add: then if want to build the snippet during the search query (not as a seperate CALL query), can use SNIPPET() function in the intial select
http://sphinxsearch.com/docs/current.html#sphinxql-select

Mysql search, match words if similar

I have a db with a table called sections. In that is a field called head that has a full text index with 3 entries each a string. 2 entries have the word motorcycle and one has motorcycles. I can't seem to find a way to return all 3 if the term "motorcycles" is search.
I have tried
SELECT * FROM sections
WHERE MATCH (head) AGAINST ('Motorcycles')
but it only returns the plural entry. I have also tried.
SELECT * FROM sections
WHERE head like '%motorcycles%'
but that also only returns the plural entry. Is there a way to return all three rows based on "motorcycles"?
Have you tried boolean mode?
where match (head) against ('+ Motorcycle*' in Boolean mode)
More information is here.
Your where clause has an extra "s":
SELECT * FROM sections WHERE head like '%motorcycle%'
Assuming your question is more general than the specific motorcylce example you've given...I'm not aware of a way that you can relax the contraints directly in the SQL (without a stored proc to pre process the input). I'd suggest pre processing your input with a regex to remove/replace the chars that make the word plural. Then use like in the way that you have shown on the singular version of the word.
If i have got your Questions correctly I think you want something like this:
if (SELECT count(1) FROM sections WHERE head like '%motorcycles%')>1
begin
select * FROM selections
WHERE head like '%motorcycle%'
end

MySQL query - select postcode matches

I need to make a selection based on the first 2 characters of a field, so for example
SELECT * from table WHERE postcode LIKE 'rh%'
But this would select any record that contains those 2 characters at any point in the "postcode" field right? I am in need of a query that just selects the first 2 characters. Any pointerS?
Thanks
Your query is correct. It searches for postcodes starting with "rh".
In contrast, if you wanted to search for postcodes containing the string "rh" anywhere in the field, you would write:
SELECT * from table WHERE postcode LIKE '%rh%'
Edit:
To answer your comment, you can use either or both % and _ for relatively simple searches. As you have noticed already, % matches any number of characters whereas _ matches a single character.
So, in order to match postcodes starting with "RHx " (where x is any character) your query would be:
SELECT * from table WHERE postcode LIKE 'RH_ %'
(mind the space after _). For more complex search patterns, you need to read about regular expressions.
Further reading:
http://dev.mysql.com/doc/refman/5.1/en/pattern-matching.html
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
LIKE '%rh%' will return all rows with 'rh' anywhere
LIKE 'rh%' will return all rows with 'rh' at the beginning
LIKE '%rh' will return all rows with 'rh' at the end.
If you want to get only first two characters 'rh', use MySQL SUBSTR() function
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_substr
Dave, your way seems correct to me (and works on my test data). Using a leading % as well will match anywhere in the string which obviously isn't desirable when dealing with postcodes.