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.
Related
I try to find query to find a string that 2nd character and 2nd last character both are letter m.
SELECT last_name
FROM employees
WHERE (last_name LIKE '_m%m_' AND LENGTH(last_name) >= '3');
Thanks in advance :)
Why not just OR instead of AND? I don't see the point of AND when your LIKE operator allready rules out names below three characters. You don't need to use regex nor a check for length:
SELECT last_name FROM employees
WHERE last_name LIKE '_m_'
OR last_name LIKE '_m%m_';
The use of OR and LIKE does catch any string that has at least 3 characters.
If you must use regex, try REGEXP operator:
SELECT last_name FROM employees WHERE last_name REGEXP '^.m(.*m)?.$';
Where the pattern means:
^.m - Start-line anchor with a single character and a literal 'm';
(.*m)? - Optional capture group to match 0+ characters upto a literal 'm';
.$ - A single character with end-line anchor.
The benefit of REGEXP is that it's a bit less verbose if you need case-insensitive matching using pattern: '^.[Mm](.*[Mm])?.$'. See an online demo.
If you need all record with second and last character is m you can use the following query:
select * from <table> where <column> like '_m%m'
the _ in the query is a placeholder for one character and % for many characters
Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'
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.
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.
i have a notes column which contains text and has an id within the text, something like
"some random text (actvityid - 1234)"
i need to pull out the id 1234 in this case and update the activityid column within the same table.
my query looks like this
"UPDATE table_name SET activityId = {$f['activityId']} WHERE notes REGEXP '{$f['activityId']}' "
the problem with this is if $f['activityId'] is 34 or 123 for example it still updates the activityid column with that value. How can i do an exact match on "1234" and update only if it matches the whole string, here "1234".
Many thanks.
WHERE notes REGEXP CONCAT('(actvityid - ', {$f['activityId']}, ')')
or
WHERE notes REGEXP '[[:<:]]{$f['activityId']}[[:>:]]'
[[:<:]] and [[:>:]] stands for word boundaries.
No need to use CONCAT if variable is passed from PHP,
and no need to use REGEXP if you match exact string without special characters
WHERE notes LIKE '%(actvityid - {$f['activityId']})%'