如何在子查询中使用Like?

时间:2017-05-20 11:14:59

标签: sql sql-server

我正在尝试获取EmployeeId,其first,lastname或middlename包含传递的值。

这是我的代码。

if(context.testCase.getPropertyValue("KeyName") == "employee_name")
{
if(context.testCase.getPropertyValue("SearchType") == "Alert"){
key = context.testCase.getPropertyValue("PassedValue")
if(context.testCase.getPropertyValue("Operator")=="Contains"){
def  res = sql.rows("SELECT AlertID FROM AlertEmployeeRel where EmployeeID=(Select ID from Employee where [FirstName] like '%$key%' or [LastName]='%$key%' or [MiddleName]='%$key%')")
log.info(res.AlertID)
context.testCase.setPropertyValue("tempPassedValue",res.AlertID.toString())
}

这不起作用..获取以下错误

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

任何人都可以告诉我如何在SubQuery中使用Like吗?

2 个答案:

答案 0 :(得分:2)

该问题与The simple sum 5 + 4 = 9无关。使用LIKE代替IN

=

答案 1 :(得分:0)

如果需要考虑性能,可以使用EXISTS运算符。在处理大量数据时,EXISTS通常比IN更有效,因为它会在第一次匹配后停止搜索值。另外,请确保对LastName和MiddleName使用like运算符。

SELECT aerAlertID
FROM AlertEmployeeRel aer
WHERE aer.EmployeeID EXISTS (SELECT *
                      FROM Employee e
                      WHERE e.ID = aer.EmployeeID
                      AND (e.FirstName like '%$key%'
                      OR e.LastName like '%$key%'
                      OR e.MiddleName like '%$key%')
                      );