简体   繁体   English

在表格的所有列上搜索关键字

[英]Search keywords on all column of the table

I have Vehicle_parts table like below,我有下面的Vehicle_parts表,

Vehicle_ID Vehicle_ID Vehicle_Major_Part_Number Vehicle_Major_Part_Number Vehicle_Sub_Part_Number Vehicle_Sub_Part_Number
1 1 11111 xxx 11111 xxx 22222 xxx 22222 xxx
2 2 33333 zzz 33333 兹兹 11111 yyy 11111 年年
3 3 44444 aaa 44444 aaa 22222 bbb 22222 BB
4 4 22222 zzz 22222 兹兹 11111 yyy 11111 年年
5 5 88888 zzz 88888 zzz 44444 yyy 44444 年年

I need to do keywords search on entire column of the table.我需要对表格的整个列进行关键字搜索。

Example: If the Keywords: '11111' & '88888' then the result will be like below table,示例:如果关键字: “11111”和“88888”,那么结果将如下表所示,

Vehicle_ID Vehicle_ID Vehicle_Major_Part_Number Vehicle_Major_Part_Number Vehicle_Sub_Part_Number Vehicle_Sub_Part_Number
1 1 11111 xxx 11111 xxx 22222 xxx 22222 xxx
2 2 33333 zzz 33333 兹兹 11111 yyy 11111 年年
4 4 22222 zzz 22222 兹兹 11111 yyy 11111 年年
5 5 88888 zzz 88888 zzz 44444 yyy 44444 年年

I tried Like statement in where clause but the ** where** clause not allowing to search multiple columns.我在where子句中尝试了Like语句,但 ** where** 子句不允许搜索多个列。

Also I tried Functions like below,我也尝试了下面的函数

Create function [Keyword_Search](@Keyword Varchar(100))

Returns table

as

RETURN(SELECT * FROM Vehicle_parts WHERE 
      [Vehicle_Major_Part_Number] LIKE @Keyword
      or [Vehicle_Sub_Part_Number] LIKE @Keyword

SELECT * FROM Keyword_Search ('%11111%')

Here, I could not provide multiple keywords at a time for searching.在这里,我无法一次提供多个关键字进行搜索。

Kindly suggest me to the efficient way for searching the multiple keywords on all columns present in the table.请建议我在表格中的所有列上搜索多个关键字的有效方法。

You can put multiple LIKE conditions in the WHERE clause that check every condition you named.您可以在WHERE子句中放置多个LIKE条件,以检查您命名的每个条件。 According to your description, this query will work:根据您的描述,此查询将起作用:

SELECT vehicle_id, vehicle_major_part_number, vehicle_sub_part_number
FROM vehicle_parts
WHERE vehicle_major_part_number LIKE '11111%'
OR vehicle_major_part_number LIKE '88888%'
OR vehicle_sub_part_number LIKE '11111%'
OR vehicle_sub_part_number LIKE '88888%';

Queries with LIKE and especially with % (to mark further characters might follow) are often slow and as you see, they can be long and bad to read, too.带有LIKE的查询,尤其是带有 % 的查询(用于标记后面可能出现的更多字符)通常很慢,而且如您所见,它们也可能很长而且难以阅读。 You could also try something like this:你也可以尝试这样的事情:

SELECT vehicle_id, vehicle_major_part_number, vehicle_sub_part_number
FROM vehicle_parts
WHERE CONCAT(vehicle_major_part_number, vehicle_sub_part_number) LIKE '%11111%' OR 
CONCAT(vehicle_major_part_number, vehicle_sub_part_number) LIKE '%88888%';

This might be a bit better to read, but still slow.这可能会更好阅读,但仍然很慢。

If you do exactly know which string you want to check, this will make your query much shorter, easier to read and will improve the execution time.如果您确实知道要检查哪个字符串,这将使您的查询更短,更易于阅读,并会缩短执行时间。 As example:例如:

SELECT vehicle_id, vehicle_major_part_number, vehicle_sub_part_number
FROM vehicle_parts
WHERE '11111 xxx' IN (vehicle_major_part_number, vehicle_sub_part_number)
OR '88888 zzz' IN (vehicle_major_part_number, vehicle_sub_part_number);

This will of course get two rows less than the other options, but maybe you will find a way to prevent the usage of LIKE .这当然会比其他选项少两行,但也许你会找到一种方法来防止使用LIKE This would be much better.这样会好很多。

Here a fiddle according to your example: db<>fiddle根据您的示例,这里有一个小提琴: db<>fiddle

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

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