简体   繁体   English

如何在 MySQL LIKE 子句中使用用户变量?

[英]How to use a user variables in MySQL LIKE clause?

I am trying to setup a few simple SQL scripts to help with some short term DB administration.我正在尝试设置一些简单的 SQL 脚本来帮助进行一些短期的数据库管理。 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.我遇到的问题特别是 LIKE 子句。

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.所以我想让它根据变量中的 email SET 查找结果。 The query works if I manually enter the email into the LIKE clause.如果我在 LIKE 子句中手动输入 email,则查询有效。

How can I get the LIKE clause to work with the user variable?如何让 LIKE 子句与用户变量一起使用?

UPDATE: @dems's answer works for this simple case, but I'm having trouble with a more complex query.更新:@dems 的回答适用于这个简单的案例,但我在处理更复杂的查询时遇到了问题。

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"给我错误“FUNCTION mydb.CONCAT 不存在”

The query works without the CONCAT():查询在没有 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
SET @email = 'test@test.com';

SELECT email from `user` WHERE email LIKE CONCAT('%', @email, '%');

Works without concat(): 无需concat()即可工作:

LIKE '%{$var}%'

(Mysql version 5.+) (MySQL版本5. +)

No need to CONCAT just use '%'+ Variable +'%': 无需CONCAT,只需使用'%'+变量+'%':

SET @email = 'test@test.com';
SELECT email from `user` WHERE email LIKE '%' + @email + '%';

使用与oracle相同的语法似乎可行:

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

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;

I resolved using the CONCAT function before using LIKE statement: 我在使用LIKE语句之前已解决了使用CONCAT函数的问题:

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

It works fine for me 这对我来说可以

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

评论已删除评论已删除

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM