63

I am trying to setup a few simple SQL scripts to help with some short term DB administration. As such, I'm setting up variables to try to make it easier to reuse these scripts.

The problem I'm having is specifically with the LIKE clause.

SET @email = 'test@test.com';

SELECT email from `user` WHERE email LIKE '%@email%';

So I want to have it finding results based on the email SET in the variable. The query works if I manually enter the email into the LIKE clause.

How can I get the LIKE clause to work with the user variable?

UPDATE: @dems's answer works for this simple case, but I'm having trouble with a more complex query.

SET @email = 'test@test.com';

SELECT project.projectno, project.projectname, login.username, 
CONCAT(login.firstname, ' ', login.lastname), projectuser.title 
FROM projectuser 
INNER JOIN login ON projectuser.uid = login.uid 
LEFT JOIN project ON projectuser.pid = project.pid
WHERE login.username LIKE CONCAT ('%', @email, '%')

Gives me the error "FUNCTION mydb.CONCAT does not exist"

The query works without the CONCAT():

SET @email = 'test@test.com';

SELECT project.projectno, project.projectname, login.username, 
CONCAT(login.firstname, ' ', login.lastname), projectuser.title 
FROM projectuser 
INNER JOIN login ON projectuser.uid = login.uid 
LEFT JOIN project ON projectuser.pid = project.pid
WHERE login.username LIKE @email
starball
  • 20,030
  • 7
  • 43
  • 238
CLo
  • 3,650
  • 3
  • 26
  • 44
  • 1
    Remove space character between `CONCAT` and brace `(`. It should be `CONCAT(...`, not `CONCAT (` – a1ex07 Dec 16 '11 at 17:21
  • @a1ex07 Yes, removing that space fixed it. Thanks. – CLo Dec 16 '11 at 17:24
  • I know we don't teach grammar here but surely I can't be the only one bothered by the error in the question title, "How to use **an user variables** in MySQL LIKE clause?" – qualebs Jul 08 '17 at 19:33
  • @qualebs that happened as I was editing the question at some point. Must have been late. I'd suggest you go ahead and post an edit to the question. – CLo Jul 09 '17 at 20:21

7 Answers7

135
SET @email = 'test@test.com';

SELECT email from `user` WHERE email LIKE CONCAT('%', @email, '%');
MatBailie
  • 83,401
  • 18
  • 103
  • 137
  • This works for this simpler case. I expanded my query to include some JOINS and now it's giving me an error telling me that "FUNCTION mydb.CONCAT does not exist". I've also found that (SET @email = '%test@test.com%';) allows me to skip the CONCAT, but I don't like pairing the search syntax with the data. – CLo Dec 16 '11 at 17:07
  • @Chris - You need to show your new query as a whole then, as there must be some new error in it. CONCAT() just joins multiple strings together, and in my example uses a variable as one of the strings. – MatBailie Dec 16 '11 at 17:09
  • Yes, I updated the original question with the more complex query. – CLo Dec 16 '11 at 17:14
  • 1
    @Chris - No idea, it should work. A desperate grasp into the wild would be to remove the space between `CONCAT` and `(` – MatBailie Dec 16 '11 at 17:22
  • Smart solution, Thanks :) – Minkesh Jain Feb 08 '17 at 10:02
  • `name like CONCAT("%", %s, "%")` parameterization using in Python – Johnny Apr 29 '18 at 02:59
  • Although this is the best solution, but I faced an unexpected error and found solution here: https://stackoverflow.com/questions/11770074/illegal-mix-of-collations-utf8-unicode-ci-implicit-and-utf8-general-ci-implic – Abhi Aug 06 '21 at 10:32
4

You may have error

Error Code: 1267. Illegal mix of collations for operation 'like'    0.016 sec

In this case you need to specify the same collation as used for your table like that:

SET @email = 'test@test.com' COLLATE utf8_unicode_ci;
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
0

I resolved using the CONCAT function before using LIKE statement:

SET @email = 'test@test.com';
set @email = CONCAT('%',@email,'%');
SELECT email from `user` WHERE email LIKE @email;

It works fine for me

0
BEGIN 
    SET @emailid = CONCAT('%', 'email@email.com' ,'%');
    SET @t1 =CONCAT('SELECT * FROM user WHERE email LIKE ''', @emailid, '''');
    PREPARE stmt3 FROM @t1;
    EXECUTE stmt3;
    DEALLOCATE PREPARE stmt3;
END
Amey Vartak
  • 309
  • 2
  • 5
0

Jan '22 I was having an issue getting the LIKE command to work. I discovered through echoing the variable value and my SQL statement that the quote function puts tick marks around the variable. code to get business type:

isset($_POST['button']) ? $businesstype = get_post($pdo, 'button'): 
$businesstype  = get_post($pdo, 'businesstype');

function get_post($pdo, $var)
  {
    return htmlentities($pdo->quote($_POST[$var]));
  }

echo of $businesstype: 'Barber'

echo of SQL: Select BusinessName, MemberNum, Street, City FROM member WHERE verified = TRUE AND businesstype LIKE 'Barber' Order by featurelevel desc, membershipdate desc,businessname

Since the single quotes were already around the search value, to use LIKE I had to get the % after the first tick mark, and before the last. I used a combination of a string replace function and concat operators: replacing the original single quotes with nothing, and concatting the '% and %' around the search value.

$businesstype ="'%" . str_ireplace("'","",$businesstype) . "%'";

. Worked for me.I hope it helps someone. Let me know if I am undoing the SQL injection safeguard somehow.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Greg
  • 1
  • 1
-1

Using same syntax as oracle seems to work:

SELECT email from user WHERE email LIKE '%' || @email || '%';

-1

No need to CONCAT just use '%'+ Variable +'%':

SET @email = 'test@test.com';
SELECT email from `user` WHERE email LIKE '%' + @email + '%';
kurdtpage
  • 3,142
  • 1
  • 24
  • 24
Anand Maurya
  • 170
  • 1
  • 1
  • 12