简体   繁体   English

SQL从表中选择,其中列包含特定的字符串

[英]SQL select from table where column contains specific strings

I have a table which contains strings which is needed to be searched [like below image] 我有一个表,其中包含需要搜索的字符串(如下图所示)

搜索表 TableA 表A

After that, I have a table which stores data [like below image] 之后,我有一个表来存储数据(如下面的图片所示)

在此处输入图片说明 TableB 表B

And what the search function does is to select from TableB only if the record contains the string in TableA [ie The expected result should be like below image(TableC)] 搜索功能的作用是仅在记录包含TableA中的字符串的情况下才从TableB中选择[即,预期结果应类似于image(TableC)

在此处输入图片说明 TableC 表C

I've tried using the SQL below, but the SQL had some error while trying to run [ Incorrect syntax near 'select' ] , also, the SQL is a bit complicated, is there any way to make the SQL simplier? 我尝试使用下面的SQL,但是尝试运行[ Incorrect syntax near 'select' ]时,SQL出现了一些错误,而且SQL有点复杂,有什么方法可以使SQL更简单吗?

select * from TableB a 
where exists 
    (select colA from TableB b 
        where b.colA = a.ColA and Contains (b.ColA, select searchCol from TableA))

Please try: 请试试:

select 
  a.* 
From tblA a inner join tblB b 
    on b.Col like '%'+a.Col+'%'

SQL Fiddle Demo SQL小提琴演示

One of these should work according to me 其中之一应该根据我的工作

SELECT b.colA 
FROM TableB b join TableA a
WHERE colA like '%' + a.ColA + '%';


SELECT b.colA
FROM TableB b join TableA a
WHERE INSTR(b.colA, '{' + a.colA + '}') > 0;
SELECT b.* FROM tblA a,tblB b
WHERE PATINDEX('%'+a.Col+'%',b.Col)>0

SQL FIDDLE DEMO SQL FIDDLE DEMO

I have searched for performance difference between PATINDEX and LIKE but not got a conclusive answer , some say PATINDEX is faster when indexing can't be used which is when pattern to be searched is enclosed within wildcard character eg '%'+a.Col+'%' while other post mention they can similar performance . 我已经搜索了PATINDEX和LIKE之间的性能差异,但没有得出确切的答案,有人说当不能使用索引时PATINDEX更快,这是当要搜索的模式包含在通配符中时,例如'%'+ a.Col +' %',而其他帖子提到他们可以达到类似的效果。

Perhaps some SO SQL Wizard can look in crystal ball and show us the light 也许有些SO SQL向导可以看水晶球并向我们展示光

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

相关问题 如何从SQL的表中选择,其中一列中的元素仅包含另一列的特征,而另一列本身又包含两个特征? - How to select from a table in SQL where element in a column contains only one feature of another column which in turn contains two features in itself? SQL-从表中选择所有行,其中一列仅包含数字 - SQL - select all rows from table where a column contains only digits SQL注入-从表中的*中选择列 - SQL Injection - select * from table where column in ( SQL 选择行,其中列包含字符串中的任何单词 - SQL select rows where column contains any of word from string Bigquery从表中选择任何列包含'FINISHED' - Bigquery Select from table where any column contains 'FINISHED' SQL SELECT其中列CONTAINS子字符串 - SQL SELECT where column CONTAINS substring 从3列表中选择所有记录,其中2个特定列中有重复项 - Select all records from a 3 column table where there is duplication in 2 specific columns 如何从SQL表中选择WHEST TIMESTAMP是一个特定的Date - How to select from SQL table WHERE a TIMESTAMP is a specific Date 从其他表的串联中选择SQL Server中的特定列 - Select a specific column in SQL Server from a concatenation from a different table 如何从表中获取包含 sql 或 python 的字符串列表列的表 - How to get table from table contains list column of strings by sql or python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM