MySQL - Like multiple parts of a string - mysql

Is it possible in MySQL to do a 'like' in the statement that matches any part of a string.
It's hard to explain but I can give an example.
I am concatenating two columns together to display a full name (e.g John Barry Smith) the first name is 'John Barry' the last name is 'Smith'
I am making a filter and I would like it where I can type in 'John Smith' , which will then be passed through as the 'like' (or something similar) in the where clause and it will bring up the user rather than having to type 'John Barry' as not all of the users in the table have a middle.
Hopefully i've been clear enough in my question.

You could do something like this:
WHERE CONCAT(first_name,' ', last_name) LIKE ('john%smith')
To list all "John sth sth Smith"s
Or even LIKE ('%john%smith%') to list al "sth sth John sth sth Smith sth"s (just replace all spaces with %'s)
An other option would be to split your search on spaces and do a LIKE for each word to list al Johns, and all Smiths, but this would be irrelevant I think.

Related

How to select from multiple MySQL columns using wildcards and spaces?

I have a typeahead select box for finding users in a database. Frequently there are meny users with the same first name & different last names.
A person searching for a user would generally type in: "John doe".
The problem with this is that the typeahead will start getting all the John users but then return no results when the space is typed in.
"WHY" this is happening is obvious, user first names are trimmed & anything with a space will not match ~ let alone with another character following that, first & last names are stored in separate columns.
Here is the query as it stands:
SELECT Entities.* FROM `Entities`
WHERE `Entities`.`first_name` LIKE '%john doe%'
OR `Entities`.`last_name` LIKE '%john doe %'
How can I re-write this query so that the results would be:
all users whose first name is john
ignore any amount of spaces
AND where last name is doe
when a user types in"john doe" in the search control
(Keeping in mind that the search control is ONE text type field)
If you want the first name like john and the last name like doe, then try something like:
where concat(Entities`.`first_name`, ' ', `Entities`.`last_name`) like '%john% doe%'
If you want do do this based on a search string, then something like:
where concat(`Entities`.`first_name`, ' ', `Entities`.`last_name`) like
concat('%', replace('john doe', ' ', '%'), '%')
Or, use full text search.

SQL Text Search - EXACT before space, LIKE after in search term

I'm trying to create a SQL query which will supply values for auto completion for a text field. Everything is working however I can't seem to create an SQL query which is exact enough for the purposes I want. I am using MySQL.
If there is a space (or multiple spaces) in the search term, I only want the query to do a LIKE comparison on the part of the string after the last space.
For example, say I have two possible values in the database:
Bolt
Bolts Large
Currently if the user types 'Bolt' then a space, both values above are returned using this query -
SELECT name FROM items WHERE name LIKE 'SEARCH_TERM%'
What I want is that if the user types 'Bolt' then a space, then only Bolt is returned from the database.
Effectively meaning that only the last part of the search term after the space is compared using LIKE, the results should match exactly up until the last space.
I've also tried:
SELECT name FROM items WHERE name LIKE 'SEARCH_TERM[a-z]%'
But that actually returns no results using the above scenario.
Is what I'm after possible? I've also tried to explore using Full Text Search but have had no look with that. I believe full text search is enabled on the name field, however I have limited experience with this. The query below didn't work.
SELECT name FROM items WHERE MATCH(name) AGAINST('SEARCH_TERM')
Any advice or points would be very appreciated.
The query
SELECT name FROM items WHERE name LIKE 'Bolt %'
doesn't return any record, because both 'Bolt' and 'Bolts Large' don't match 'Bolt %'.
SELECT name FROM items WHERE name LIKE 'Bolt%'
returns both records, because both 'Bolt' and 'Bolts Large' match 'Bolt%'.
To look for 'Bolt' and not 'Bolts', you must add a space to both your search string and the column string:
SELECT name FROM items WHERE concat(name, ' ') LIKE 'Bolt %'
returns 'Bolt' but not 'Bolts Large'.
SELECT name FROM items WHERE REPLACE(name, ' ', '') LIKE 'SEARCH_TERM%'
You could also use CONCAT and TRIM, or just trim
SELECT name FROM items WHERE name LIKE TRIM('SEARCH_TERM')
or your choice
SELECT name FROM items WHERE name LIKE CONCAT(TRIM('SEARCH_TERM'), '%')
SELECT name FROM items WHERE name LIKE CONCAT('%',TRIM('SEARCH_TERM'))
SELECT name FROM items WHERE name LIKE CONCAT('%',TRIM('SEARCH_TERM'), '%')

How does the Like operator behave with an empty argument (%%)?

Using the like operator
SELECT first_name, last_name FROM student_details WHERE first_name LIKE 'S%';
will provide the name which start with S.
But
SELECT first_name, last_name FROM student_details WHERE **first_name LIKE '%%';**
%% - It didn't have any string constraints. This query is returning the complete list.
How is LIKE %% processed in SQL?
Can anyone clarify this?
The % sign is considered the zero-or-more repetition wildcard character in SQL. The way it works is pretty much the same as in any other areas of computing, you can have a wide definition and explaination of how it works just putting it in Google.
That's why it returns all the entries in your database, because it matches anything even if it isn't repeated a time (by the way, the same behavior you'll achieve with LIKE '%').
The one and just one repetition wildcard in SQL is usually represented with _.
If do not pass any search string then LIKE will return all values
You have to see this link
The % signifies zero or more occurences of whatever characters.
So first_name like '%%' (or first_name like '%' for that matter) will return all records where first_name contains zero or more characters. That means all records, except those where first_name is NULL.

MySQL search match at least one word on query string

i'm making a query in mysql but i have a problem: this is my column structure
|country|
--------
Boston
--------
Chicago
--------
washington
The thing is i may have a search item like:
North Washington
Boston Easht
South Chicago
So i'm trying to match it using the %like% operador like that:
select * from am_ciudad where Ciu_Nombre LIKE '%NORTH BOSTON';
select * from am_ciudad where Ciu_Nombre LIKE 'CHICAGO%';
select * from am_ciudad where Ciu_Nombre LIKE '%South Chicago%';
the second one makes match because it starts with "chicago" word, but in the case of the query has a prefix it doesn't, is there a way to search by at least one match in the query string?
IN method
Use comma separated list of your search query:
SELECT * FROM am_ciudad WHERE Ciu_Nombre IN('North', 'Washington', ...)
REGEXP method
I can imagine the REGEXP will be slower, but I haven't benchmarked it.
SELECT * FROM am_ciudad WHERE Ciu_Nombre REGEXP(North|Washington|...)
Your other searches won't match because they do not exist.
If you want to match Boston in the phrase I love Boston Red Sox then you would need to use ...LIKE '%Boston%'; the %s are wild cards so using them before and after the word you are tying to match tell the query that you don't care what come before and after. Your search string of ...LIKE '%NORTH BOSTON'; is telling query that you are looking for <anything>North BOSTON; which obviously you don't have.
Hopefully that makes sense and helps out.
S
Im not sure if your version of mysql supports it but its worth trying.
select * from am_ciudad where Ciu_Nombre in (NORTH,BOSTON);
Same for the others, just replace the space with ','

MySQL string match with multiple words

This page has a great example using REGEXP to do pattern matching. the problem with REGEXP won't match the following strings:
"Mr John"
"Dr. John"
or even:
"Mr. John Doe"
with the string "John Doe"
I would like to know how do I get positive matches for any of the given examples?
Here is a sample code:
Drop table Names;
CREATE TABLE Names (
first_name VARCHAR(20),
last_name VARCHAR(20)
);
INSERT INTO Names VALUES ('John','Doe');
INSERT INTO Names VALUES ('Sue','Yin');
INSERT INTO Names VALUES ('Diego James', 'Franco');
select * from Names;
/*To find names containing a string */
/*I want this to march John Doe*/
SELECT * FROM Names WHERE first_name REGEXP 'Mr John';
/*This has John misspelled, I want it to match John Doe */
SELECT * FROM Names WHERE first_name REGEXP 'Hohn' AND last_name REGEXP 'Doe';
/*And this would match Diego James Franco*/
SELECT * FROM Names WHERE first_name REGEXP 'Dr Diego' AND last_name REGEXP 'Franco';
-Thank you
UPDATE:
Thank you for the answers, the question is not how to use regular expression to do the matching that I want, but rather how can I do it regardless of REGEXP. I use REGEXP as an example of pattern matching. I do appreciate the clarification on regular expressions.
Regular expressions are not meant to match inexact strings (for example, a spelling error). It seems like that is what you are trying to do. A regular expression could be used, for example, to match any social security number (three digits followed by a hyphen followed by two digits followed by another hyphen followed by four digits). But you couldn't use a regular expression to match misspellings of John. Misspellings are handled using some fancier coding, usually called n-gram matching (see: http://en.wikipedia.org/wiki/N-gram). If you are also using Ruby-on-Rails, there is a great Gem (called Chrononaut-no_fuzz) to handle this for you, but with plain MySQL you may have to hand-code this feature.
the string John Doe should match is the last one. Can you please post the exact sql and the data it's trying to match
Ok, so you have the string and pattern mixed up you supply a pattern to regexp intending to match a string. So for example Dr. John will never match John since the pattern tries to match Dr and fails. However John will match Dr John since the pattern now find John within Dr John. My suggestion to you is to read a regular expressions primer.