SQL:在子字符串上连接表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9788599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL: Join tables on substrings
提问by Elmex
I have a table Awith the string-column aand a table Bwith the string-column b. a is a substring of b. Now I want to join the the two tables on a and b. Is this possible?
我有一个表阿与绳柱一和表乙与串列b。a 是 b 的子串。现在我想加入a 和 b上的两个表。这可能吗?
I want something like this:
我想要这样的东西:
Select * from A,B where A.a *"is substring of"* B.b
Select * from A,B where A.a *"is substring of"* B.b
How can I write this in SQL (Transact-SQL)?
如何在 SQL (Transact-SQL) 中编写它?
回答by Chris
declare @tmp1 table (id int, a varchar(max))
declare @tmp2 table (id int, b varchar(max))
insert into @tmp1 (id, a) values (1,'one')
insert into @tmp2 (id,b) values (1,'onetwo')
select * from @tmp1 one inner join @tmp2 two on charindex(one.a,two.b) > 0
You can also use charindex, 0 means its not found, greater than 0 is the start index
也可以使用charindex,0表示未找到,大于0是起始索引
回答by masoud Cheragee
set an inner join on a substring(4 letters) of FIELD1 of table TABLE1 with FIELD1 of table TABLE2
将表 TABLE1 的 FIELD1 的子字符串(4 个字母)与表 TABLE2 的 FIELD1 设置内连接
select TABLE1.field1,TABLE2.field1 from TABLE1 inner join TABLE2 on substring(TABLE1.field1,2,5)=TABLE2.field1
回答by barsju
You have the contains function: http://msdn.microsoft.com/en-us/library/ms187787.aspx
你有包含功能:http: //msdn.microsoft.com/en-us/library/ms187787.aspx
select * from A,B where contains(B.b, A.a)
回答by Euclides Mulémbwè
try this:
尝试这个:
Select * from A,B where B.b LIKE '%'+A.a+'%'