Combine two columns in SQL for WHERE clause - mysql

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

Related

MySQL - Like multiple parts of a string

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.

How to search strings with an alternate character?

I have a field where I save strings, like:
"one two-tree"
"one-two-tree"
"one-two tree"
"one two tree"
When I do a SELECT, I want to retrieve strings that have either "-" or " " (space). Example:
When I do:
Select name from table where name="one two tree"
I want it to bring also results where there is either space or -, in this case returning all string exemplified above.
Is there a wildcard for this?
As far as standard SQL, you must use "or", or "like". depending on what exactly you want. EXAMPLE: Select name from table where name like "one?two?tree".
However, mySQL supports a REGEX extension that will give you what you want:
http://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
One option is to use replace:
select name
from yourtable
where replace(name, '-', ' ') = 'one two tree'
There is, but it is slow: you can use REGEXP:
SELECT name
FROM table
WHERE name REGEXP 'one[- ]two[- ]tree'
or you can use replacements:
SELECT name
FROM table
WHERE REPLACE(name, '-', ' ') = 'one two three'
but your best bet is to make an additional column where you will have a normalised name (with dashes always replaced with spaces, for example) so you can take advantage of indices.
You can use the LIKE condition with '%' (wildcard operator) for this.
e.g.
SELECT name from table_name WHERE name LIKE '%-%' OR name LIKE '% %'
-- will return all names that have `-` or ` `.

sql select where string matches pattern defined in another table

I have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)
Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch
You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.

MySql Query on parts on string

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[[:>:]]'

sql query not working with concat

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.