SQL LIKE 仅适用于全名
SQL LIKE is only working With Full Name
在我的一个 sp 中有以下代码行
select distinct (a.itemid), a.itemcode, v.itemdescription
from aitem a
INNER JOIN vwitemdescription v ON a.itemID = v.itemID
WHERE a.active=1
-----------------------------------------
AND (@ItemDesc like '%'+ a.itemdescription +'%')
-----------------------------------------
如果我在完整描述中给@ItemDesc 值,我得到值,如果我在半描述中给@ItemDesc 值,我在 return.
中什么也得不到
例如:
如果我给
@ItemDesc = 'Cow Dung - '
我得到的结果是
---------------------------------------
|itemid | itemcode | itemdescription |
--------------------------------------
| 63 | 40-17005 | Cow Dung - |
---------------------------------------
即使我将字符串剪切为 @ItemDesc = 'Cow Dung'
我也会得到相同的结果,
但是,如果我将其切割成 @ItemDesc = 'Cow'
或仅切割成单个字符,我不会得到任何结果。
即使只输入一个字符,我也想加载该项目。
我的代码有什么问题吗?如何正确处理?
您需要切换 LIKE
表达式中的项目:
AND a.itemdescription LIKE '%' + @ItemDesc + '%'
使用此逻辑,itemdescription
的任何 子串 都会匹配。例如,以下是真实条件:
AND `Cow Dung - ` LIKE '%Cow%'
这是完整的查询:
SELECT DISTINCT
a.itemid, a.itemcode, v.itemdescription
FROM aitem a
INNER JOIN vwitemdescription v
ON a.itemID = v.itemID
WHERE
a.active = 1 AND
a.itemdescription LIKE '%' + @ItemDesc + '%';
比较变量应始终位于语句的右侧。您的新查询应如下所示。
select distinct (a.itemid), a.itemcode, v.itemdescription
from aitem a
INNER JOIN vwitemdescription v ON a.itemID = v.itemID
WHERE a.active=1
--changed condition start
AND (a.itemdescription like '%'+ @ItemDesc +'%')
--changed condition end
在我的一个 sp 中有以下代码行
select distinct (a.itemid), a.itemcode, v.itemdescription
from aitem a
INNER JOIN vwitemdescription v ON a.itemID = v.itemID
WHERE a.active=1
-----------------------------------------
AND (@ItemDesc like '%'+ a.itemdescription +'%')
-----------------------------------------
如果我在完整描述中给@ItemDesc 值,我得到值,如果我在半描述中给@ItemDesc 值,我在 return.
中什么也得不到例如:
如果我给
@ItemDesc = 'Cow Dung - '
我得到的结果是
---------------------------------------
|itemid | itemcode | itemdescription |
--------------------------------------
| 63 | 40-17005 | Cow Dung - |
---------------------------------------
即使我将字符串剪切为 @ItemDesc = 'Cow Dung'
我也会得到相同的结果,
但是,如果我将其切割成 @ItemDesc = 'Cow'
或仅切割成单个字符,我不会得到任何结果。
即使只输入一个字符,我也想加载该项目。
我的代码有什么问题吗?如何正确处理?
您需要切换 LIKE
表达式中的项目:
AND a.itemdescription LIKE '%' + @ItemDesc + '%'
使用此逻辑,itemdescription
的任何 子串 都会匹配。例如,以下是真实条件:
AND `Cow Dung - ` LIKE '%Cow%'
这是完整的查询:
SELECT DISTINCT
a.itemid, a.itemcode, v.itemdescription
FROM aitem a
INNER JOIN vwitemdescription v
ON a.itemID = v.itemID
WHERE
a.active = 1 AND
a.itemdescription LIKE '%' + @ItemDesc + '%';
比较变量应始终位于语句的右侧。您的新查询应如下所示。
select distinct (a.itemid), a.itemcode, v.itemdescription
from aitem a
INNER JOIN vwitemdescription v ON a.itemID = v.itemID
WHERE a.active=1
--changed condition start
AND (a.itemdescription like '%'+ @ItemDesc +'%')
--changed condition end