像存储过程参数一样使用 SQL LIKE 运算符 [英] Using SQL LIKE operator like a Stored Procedure paramater
问题描述
我正在尝试在存储过程中使用 LIKE %...% 但我不确定如何在运算符中使用传入变量.例如,我正在这样做:
I'm trying to use LIKE %...% in a stored procedure but I'm not sure how to use the incoming variable in the operator. For example, I'm doing this:
DELIMITER //
CREATE PROCEDURE GetGameByName(IN gameName varchar(255))
BEGIN
SELECT *
FROM game
WHERE gameTitle LIKE '% + gameName + %';
END //
DELIMITER ;
但是当我这样称呼它时
CALL GetGameByName('Creed');
它什么也没有返回(我确实有一个游戏标题为刺客信条"
It is returning nothing (I DO have a game with gameTitle "Assassin's Creed"
知道我做错了什么吗?谢谢
Any ideas what I'm doing wrong? Thanks
推荐答案
由于评论中的问题而更新 - 解决方案现在是 WHERE gameTitle LIKE CONCAT('%',gameName,'%')
UPDATED due to issue in comment - the solution is now WHERE gameTitle LIKE CONCAT('%',gameName,'%')
====== 上一个答案 ======
===== Previous Answer =====
在我看来,您好像忘记了引号.而不是 WHERE gameTitle LIKE '% + gameName + %';
,你应该做 WHERE gameTitle LIKE '%' + gameName + '%';
It looks to me like you forgot quotation marks. Instead of WHERE gameTitle LIKE '% + gameName + %';
, you should probably do WHERE gameTitle LIKE '%' + gameName + '%';
按照您的设置方式,您将错误的通用文本 '%gameTitle%'
输入到查询中,而不是您真正想要的内容,类似于:'%Creed%'
.
The way you have it set up, you are feeding the incorrect generic text '%gameTitle%'
into the query, instead of what you really want, which is something like: '%Creed%'
.
这篇关于像存储过程参数一样使用 SQL LIKE 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!