我有一张这样的表:
ID URLIdentifier
1 google.com
2 youtube.com
3 baidu.com
和另一个像这样的表
URLID网址
1 www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=123
2 www.youtube.com/channel/UCl8dMTqDrJQ0c8y23UBu4kQ
3 www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=fadsfsafsa
4 www.baidu.com/s?wd=fdsafa&rsv_spt=1&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=0&rsv_sug3=7&inputT=1009
5 www.youtube.com/watch?v=5qanlirrRWs&list=PLbpi6ZahtOH5r5PlY8nQjm41h3UL_x8gl
6 www.youtube.com/watch?v=5aeKdmw9W50
7 v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=fdsafa&ie=utf-8&ie=utf-8
我想选择URL包含URLIdentifier的所有行,结果如下:
ID URLIdentifier URLID URL
1 google.com 1 www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=123'
1 google.com 3 www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=fadsfsafsa'
2 youtube.com 2 www.youtube.com/channel/UCl8dMTqDrJQ0c8y23UBu4kQ
2 youtube.com 5 www.youtube.com/watch?
v=5qanlirrRWs&list=PLbpi6ZahtOH5r5PlY8nQjm41h3UL_x8gl
2 youtube.com 6 www.youtube.com/watch?v=5aeKdmw9W50
3 baidu.com 4 www.baidu.com/s?wd=fdsafa&rsv_spt=1&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=0&rsv_sug3=7&inputT=1009
3 baidu.com 7 v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=fdsafa&ie=utf-8&ie=utf-8
想象一下,有数百个URLIdentifier和数百万个URL,所以我需要一个选择所有行的脚本。
答案 0 :(得分:2)
您需要使用join
将数据组合在一起。然后有几种不同的方法来获取匹配的记录。这是使用like
:
select t1.ID, t1.URLIdentifier, t2.URLID, t2.URL
from table1 t1
inner join table2 t2 on t2.URL like '%' + t1.URLIdentifier + '%'