表a
id
----------------------------------------------------------------------------------------------------
21431343
11223234
234233214
154214
664214
235412
4322
3245231
表b
id
----------------------------------------------------------------------------------------------------
2143
11223
23423
154
664
235
432
在表a的id是建了全文索引,
select a.id from a,b where a.id like b.id+'%'
我想用全文索引实现上面查询的功能:
select a.id from a,b where contains(a.id,b.id)
这样写显然是不对的,请问有没有什么办法来实现啊?游标方法除外
id
----------------------------------------------------------------------------------------------------
21431343
11223234
234233214
154214
664214
235412
4322
3245231
表b
id
----------------------------------------------------------------------------------------------------
2143
11223
23423
154
664
235
432
在表a的id是建了全文索引,
select a.id from a,b where a.id like b.id+'%'
我想用全文索引实现上面查询的功能:
select a.id from a,b where contains(a.id,b.id)
这样写显然是不对的,请问有没有什么办法来实现啊?游标方法除外
declare @a table(ID varchar(20))
declare @b table(ID varchar(20))
insert @a select 21431343
insert @a select 11223234
insert @a select 234233214
insert @a select 154214
insert @a select 664214
insert @a select 235412
insert @a select 4322
insert @a select 3245231insert @b select 2143
insert @b select 11223
insert @b select 23423
insert @b select 154
insert @b select 664
insert @b select 235
insert @b select 432
set nocount off
--select * from @a
--select * from @b
select a.id from @a a,@b b where a.id like b.id+'%'
id
--------------------
21431343
11223234
234233214
154214
664214
235412
4322(所影响的行数为 7 行)select a.id from @a a,@b b where patindex(b.id+'%',a.id)>0
id
--------------------
21431343
11223234
234233214
154214
664214
235412
4322(所影响的行数为 7 行)不太明白楼主的需求,草草发个.
id
--------------------
21431343
11223234
234233214
154214
664214
235412
4322(所影响的行数为 7 行)这也是一样的.
并不是负责任的解决方式呢?你这样做,from a,b这是做笛卡尔乘积,每个a的记录都会与b中的所有记录乘,加入a有8条记录,b有7条记录,那么乘出来就是8*7=56条,从这56条中检索需要的数据,也就是a.id like b.id+'%',那么系统就要遍历这56条记录,才能得到结果。多大的资源和性能消耗啊。时间更是消耗。
但是如果你在笛卡尔乘积之前把表排序,情况就又是另一番模样了。
select atmp.id from (select id from a order by id) as atmp,(select id from b order by id) as btmp where atmp.id like btmp.id+'%'
这样,得到的笛卡尔积的行数是不变的,还是56,但是,在where时,你会发现,系统在取到atmp.id之后,假设为1234,那么他只跟btmp.id的1开头的id进行匹配,当遇到2开头的id的时候,这次匹配结束,也就是说1234不用跟btmp的id大于1开头的那些记录比较了。你看看你少进行了多少次匹配吧。
使用全文搜索可以快速、灵活地为存储在 Microsoft SQL Server 数据库中的文本数据的基于关键字的查询创建索引。与仅适用于字符模式的 LIKE 谓词不同,全文查询将根据特定语言的规则对词和短语进行操作,从而针对此数据执行语言搜索。