日期:2014-05-18  浏览次数:20445 次

请教一个模糊嵌套查询的写法。
表book:
bookid
title 标题
content 内容

表titelkey
titelkey标题关键词

现在要将标题中含有任一标题关键词的bookid查询出来,该怎么写,谢谢~

------解决方案--------------------
SQL code

go
if OBJECT_ID('book')is not null
drop table book
go
create table book(
bookid varchar(4),
title varchar(20),
content nvarchar(20)
)
go
insert book
select '1001','javaee',null union all
select '1002','sql server',null union all
select '1003','wp7',null union all
select '1004','android 2.3.4',null union all
select '1005','linux os',null union all
select '1006','android 2.3.0',null union all
select '1007','windows os',null

go
if OBJECT_ID('titelkey') is not null
drop table titelkey
go
create table titelkey(
tk varchar(10)
)
go
insert titelkey
select 'java' union all
select 'os' union all
select 'wp'


select *,
COUNT(1)over(partition by tk) as times--每个关键词有多少本相关book
from(
select bookid,title,tk from book
cross join titelkey
where CHARINDEX(tk,title)>0)t

/*
能看懂下面的结果吧
bookid    title    tk    times
------------------------------
1001    javaee    java    1
1005    linux os    os    2
1007    windows os    os    2
1003    wp7    wp    1
*/

------解决方案--------------------
全文索引查找。