[英]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+'%'
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.