简体   繁体   English

将SQL LIKE运算符与%%一起使用

[英]Using the SQL LIKE operator with %%

I had a requirement to create a query in SQL Server where the search condition would include/exclude a table based on user input. 我需要在SQL Server中创建一个查询,其中搜索条件将包括/排除基于用户输入的表。

Say I have two tables, TABLE_A and TABLE_B with columns KEYCOLUMN_A and COLUMN_A in TABLE_A and columns FKCOLUMN_B and COLUMN_B in TABLE_B . 假设我有两个表, TABLE_ATABLE_BKEYCOLUMN_A COLUMN_A中的TABLE_AFKCOLUMN_BCOLUMN_B中的TABLE_B FKCOLUMN_BTABLE_B

And a query like: 和查询一样:

SELECT TABLE_A.* FROM TABLE_A, TABLE_B WHERE TABLE_A.KEYCOLUMN_A = TABLE_B.FKCOLUMN_B
AND TABLE_A.COLUMN_A LIKE '%SEARCH%' AND TABLE_B.COLUMN_B LIKE '%SEARCH2%'

Now if the user does not input SEARCH2 , I don't need to search TABLE_B . 现在,如果用户没有输入SEARCH2 ,我不需要搜索TABLE_B But this would mean an IF ELSE clause. 但这意味着IF ELSE条款。 And as the number of "optional" tables in the query increases, the permutations and combinations would also increase and there will be many IF and ELSE statements. 随着查询中“可选”表的数量增加,排列和组合也会增加,并且会有许多IFELSE语句。

Instead I decided to keep the statement as it is. 相反,我决定保持声明不变。 So if SEARCH2 is empty, the query will effectively become: 因此,如果SEARCH2为空,则查询将有效地变为:

SELECT * FROM TABLE_A, TABLE_B WHERE TABLE_A.KEYCOLUMN_A = TABLE_B.FKCOLUMN_B
AND TABLE_A.COLUMN_A LIKE '%SEARCH%' AND TABLE_B.COLUMN_B LIKE '% %'

Can the SQL optimizer recognize that LIKE %% is as good as removing the condition itself? SQL优化器能否识别LIKE %%与删除条件本身一样好?

Wrap an OR around your "B" table, such as: 在“B”表周围包裹OR ,例如:

AND (len(searchString)=0 OR table_b.column_b LIKE "%searchString%" )

This way, if no value for the string, its length would be zero, and the first part of the OR would be evaluated, always come back as true and return that portion of the equation as valid and ignore the other half using the LIKE clause. 这样,如果没有字符串的值,它的长度将为零,并且OR的第一部分将被评估,则总是返回true并将等式的该部分返回为有效并使用LIKE子句忽略另一半。

You could apply the same for as many linked tables as you need. 您可以根据需要为多个链接表应用相同的内容。

LIKE is used with the WHERE clause to search, update, and delete a record using wild cards. LIKE与WHERE子句一起使用,以使用通配符搜索,更新和删除记录。

Example: 例:

To search all records whose employee name is starred from a character, 'a': 要搜索员工姓名由角色加星号的所有记录,“a”:

select * from Employee where Name like 'a%'

To update all records with name amit whose employee name is starting with a character, 'a': 要更新名称为amit且员工姓名以字符开头的所有记录,“a”:

update Employee set Name='amit' where Name like 'a%'

To delete all records whose employee name is starting with a character, 'a': 要删除员工姓名以字符开头的所有记录,“a”:

delete from Employee  where Name like 'a%'

First thing, you have a space in your example: 首先,您的示例中有一个空格:

AND TABLE_B.COLUMN_B LIKE '% %'

That will never be optimized as it is indeed a significant condition. 这将永远不会被优化,因为它确实是一个重要的条件。

Now, I think that if it is optimized away depends on the database engine and how smart it is. 现在,我认为如果它被优化取决于数据库引擎以及它的智能程度。

For example, SQL Server 2005 does offer the same execution plan for the two types of queries, while MySQL 5.0.38 does not. 例如, SQL Server 2005确实为两种类型的查询提供相同的执行计划,而MySQL 5.0.38则没有。

MySQL中,您也可以使用ILIKE,然后它不区分大小写。

You can rewrite you query like this: 您可以像这样重写查询:

SELECT TABLE_A.* FROM TABLE_A, TABLE_B WHERE TABLE_A.KEYCOLUMN_A = TABLE_B.FKCOLUMN_B
AND (@paramA='' or TABLE_A.COLUMN_A LIKE '%' + @paramA + '%')
AND (@paramB='' or TABLE_B.COLUMN_B LIKE '%' + @paramB + '%')

This way, if paramA or paramB is '', then the other column that is queried inside same parentheses will not be queried. 这样,如果paramA或paramB为'',则不会查询在相同括号内查询的另一列。

Use UNION and proper JOINs. 使用UNION和正确的JOIN。

The %foo% search term is bad enough (can't use index) without adding OR and LEN to the mix too. %foo%搜索字词已经足够糟糕(无法使用索引),也没有添加OR和LEN。

SELECT
    TABLE_A.*
FROM
    TABLE_A
    JOIN
    TABLE_B On TABLE_A.KEYCOLUMN_A = TABLE_B.FKCOLUMN_B
WHERE
    TABLE_A.COLUMN_A LIKE '%SEARCH%' AND TABLE_B.COLUMN_B LIKE '%SEARCH2%'
UNION
SELECT
    TABLE_A.*
FROM
    TABLE_A
WHERE
    TABLE_A.COLUMN_A LIKE '%SEARCH%'

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column LIKE运算符在WHERE子句中用于搜索列中的指定模式

LIKE '%[p-s]'  -- "It search data from table parameter where sentence ending with p,q,r,s word."
LIKE '[0-9]' --It use for search only numeric value
LIKE '%table%' -- it use for search parameter from table where use "table" keyword'.
LIKE %[^p-r]  -- it set the condition where Not Ending With a Range of Characters

Example: 
SELECT T1.BrandName,T1.BrandID,T2.CategoryName,T2.Color FROM TABLE1 T1 
        LEFT JOIN TABLE2 T2 on T1.ID  = T2.BrandID
        WHERE T1.BrandName LIKE '%Samsung%'
Example: 
SELECT T1.BrandName,T1.BrandID,T2.CategoryName,T2.Color FROM TABLE1 T1 
        LEFT JOIN TABLE2 T2 on T1.ID  = T2.BrandID
        WHERE T1.BrandName LIKE '%[a-j]'

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

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