The name column contains items like 'John Smith', 'Elsa John Sanders', 'LilJohn Thomson', 'John'.
How can I structure a query just to return the names with John but not LilJohn.
I cannot do a LIKE '%John%' as it would return 'LilJohn Thomson'.
Looks like this is the same as:
Search for "whole word match" in MySQL
The approach uses a slick regular expression.
Assuming a word is defined by a space, you can do:
where concat(' ', col, ' ') like '% John %'
The following expression should find all "John"s
a = 'John' OR
a LIKE 'John %' OR
a LIKE '% John %' OR
a LIKE '% John'
Please note that some other tricks also work, but may severely hit performance, like
CONCAT(" ",a," ") LIKE "% John %"
Try SIMILAR TO '%+John+%' using the + space wildcard.
You can use REGEXP function.
Try this:
SELECT *
FROM tableA a
WHERE a.name REGEXP '[[:<:]]John[[:>:]]'
Related
How to return rows where a column has 2 words (that is, strings separated by a space) in it?
It must be purely using SQL.
SELECT * FROM table WHERE name (has 2 strings in it);
I dont know the names when querying. Its a big dataset. I only have to check if the name contains a spacebar basically (from a comment).
If you want to distinguish names that have two parts from one-part and three-plus-part names, you can use regular expression:
SELECT * FROM my_table WHERE name REGEXP '^[^ ]+[ ]+[^ ]+$'
This regular expression matches when the entire string consists of two non-empty parts containing no spaces, with one or more space separating them.
This perfectly works for me
You can use 'AND' condition and Like Operator with wildcards (%).
SELECT * FROM table_name WHERE name LIKE '%Word1%' AND name LIKE '%Word2%'
How about simply:
...
WHERE [name] LIKE '%Word1%'
AND [name] LIKE '%Word2%'
SELECT * FROM table WHERE concat(' ',name,' ') like '% str1 %'
AND concat(' ',name,' ') like '% str2 %'
The extra blanks are there to separate words.
You can use the following technique
mysql> select length('first name')-length(replace('first name',' ','')) as diff;
+------+
| diff |
+------+
| 1 |
+------+
i.e. get the difference of actual name and the name after replacing space, and if its 1 then you have the value as firstname lastname
So the query may look like
select * from table
where
length(col_name)-length(replace(col_name,' ','')) = 1
Use % between words.
Example:
SELECT * FROM Table WHERE Col LIKE '%word1%word2%'
SELECT * FROM table_name WHERE name LIKE '% %'
I'm trying to get all rows where the field given name ends in a space and a letter, any letter. Currently the SQL I have is
SELECT person_id FROM person_name
WHERE given_name LIKE '% M'
OR given_name LIKE '% C'
OR given_name LIKE '% A'
but instead of enumerating out all the letters I would like a query that is
given_name like '% X' where X is any letter (upper and lower case).
I tried regexp [a-zA-Z] but couldn't figure out how to tell regexp that it was the last two characters in the string. Any ideas?
I think you'd want
SELECT * FROM `person_name` WHERE `given_name` REGEX " [a-zA-Z]$"
The $ signals the end of the string.
Try something like this:
SELECT `given_name`
FROM table
WHERE `given_name` like '% _'
Try
REGEXP '[a-zA-Z]$'
$ should match the end of the string.
See http://dev.mysql.com/doc/refman/5.1/en/regexp.html for details.
Lets say I have a table simple as this:
id | name
1 | one_word
2 | two words
3 | here we have four
So, I would like to get only rows containing one word, which in the above example would only be record with id 2.
I did read the docs and tried various versions of this:
SELECT * FROM `table` WHERE name REGEXP '(.*?)\s';
so, please tell me where I'm doing it wrong.
If you are looking for the first word from a column that may have more words, you can use SUBSTRING_INDEX:
SELECT SUBSTRING_INDEX("here we have four", " ", 1)
to extract rows that don't have spaces, you can use for example this:
name NOT REGEXP '\s'
name NOT LIKE '% %'
LOCATE(' ', name) = 0
if your column can contain spaces at the beginning or at the end of the string, you could also use TRIM:
SELECT SUBSTRING_INDEX(TRIM(" here we have four "), " ", 1)
TRIM(name) NOT REGEXP '\s'
TRIM(name) NOT LIKE '% %'
LOCATE(' ', TRIM(name)) = 0
Thx to the comment from Strawberry, I came up with this:
SELECT * FROM `table` WHERE name NOT REGEXP '\s'
which works great.
What about this? In both LIKE '% %' and REGEXP case performance is bad. But i think LIKE has the least bad performance.
SELECT * FROM `table` WHERE name NOT LIKE '% %'
i am working on a search system in which a person is searched by country and their firstname and the last name. For the First and Last name i used concatbut using it with further and condition doesn't seems to work. my query is
select * from users_profile where CONCAT(fname,' ',lname) like '%v %'
or Email like '%v %' AND country='india'
in the above if ise use the query without "and" it works . but i need to specify more conditions and if i simply use and without concat that also works .
select * from users_profile where CONCAT(fname,' ',lname) like '%v %'
or Email like '%v %'
how to make it work with both concat and further and condition i want to apply.
You should not be using CONCAT, let alone any functions around your WHERE columns. Further, you should be specifying a column list, not SELECT *. Also check your order of criteria.
You can try something like this:
SELECT fullname, Email, country
FROM
(SELECT CONCAT(fname, ' ', lname) AS fullname, Email, country FROM users_profile) a
WHERE (a.fullname LIKE '%v %' OR Email LIKE '%v ') AND country = 'india'
AND has a higher precedence than OR, so you need to group your statements more logically.
Something like:
select * from users_profile where (CONCAT(fname,' ',lname) like '%v %'
or Email like '%v %') AND country='india'
should work.
In my SQL, I am using the WHERE and LIKE clauses to perform a search. However, I need to perform the search on a combined value of two columns - first_name and last_name:
WHERE customers.first_name + customers.last_name LIKE '%John Smith%'
This doesn't work, but I wondered how I could do something along these lines?
I have tried to do seperate the search by the two columns, like so:
WHERE customers.first_name LIKE '%John Smith%' OR customers.last_name LIKE '%John Smith%'
But obviously that will not work, because the search query is the combined value of these two columns.
Use the following:
WHERE CONCAT(customers.first_name, ' ', customers.last_name) LIKE '%John Smith%'
Note that in order for this to work as intended, first name and last name should be trimmed, i.e. they should not contain leading or trailing whitespaces. It's better to trim strings in PHP, before inserting to the database. But you can also incorporate trimming into your query like this:
WHERE CONCAT(TRIM(customers.first_name), ' ', TRIM(customers.last_name)) LIKE '%John Smith%'
I would start with something like this:
WHERE customers.first_name LIKE 'John%' AND customers.last_name LIKE 'Smith%'
This would return results like: John Smith, Johnny Smithy, Johnson Smithison, because the percentage sign is only at the end of the LIKE clause. Unlike '%John%' which could return results like: aaaaJohnaaaa, aaaSmithaaa.
try this:
SELECT *
FROM customers
WHERE concat(first_name,' ',last_name) like '%John Smith%';
reference:
MySQL string functions