在新聞表中增加 keyword欄位
在頁面中讓使用者輸入關鍵字時以","號分割
新聞內容
id title keyword
1 a 大,家,好
2 b 家,庭
3 c 你,好,嗎
則可以查詢出 id為2,3的新聞
create proc testfindstr --create proc( @id int --新聞id
) as
declare @temptable table(keyword varchar(50)) --create temp table
declare @tempstr varchar(50), --temp char
@i int ,--"," 號 出現位置
@tempkeywords varchar(50) --news keyword
select @tempkeywords=keyword from news where id=@id -- 關鍵字賦值
if @@rowcount=0 --如果行數為0 則返回
begin
return
endset @i=charindex(',', @tempkeywords) --第乙個,位置
while @i>=1
begin
set @tempstr=left(@tempkeywords, @i-1) --左邊起 擷取 出現位置-1
if rtrim(ltrim(@tempstr))<>'' --去左右空格 不為空
begin
insert into @temptable(keyword) values(@tempstr) --插入到臨時表
end
set @tempkeywords=substring(@tempkeywords, @i+1,len(@tempkeywords)-@i) --擷取 @i 以後字串
set @i=charindex(',', @tempkeywords) --擷取後,位置
if(@i<=0) -- 字串中如果沒有 "," 代表已經是最有乙個
begin
if rtrim(ltrim(@tempkeywords))<>'' --去左右空格 不為空 確保最後乙個字元不為,號
insert into @temptable(keyword) values(@tempkeywords) --插入剩餘字元到臨時表
endend
-- 以上語句把 "大,家,好," 關鍵字 字段 分割 存入 臨時表
select distinct id, t.keywordfrom news f,@temptable t --內聯 確保每個關鍵字被like
where f.keyword like t.keyword+',%' or f.keyword like '%,'+t.keyword or f.keyword like '%,'+t.keyword+','
ionic新聞文章
散文集朱自清 荷塘月色 節選 這幾天心裡頗不寧靜。今晚在院子裡坐著乘涼,忽然想起日日走過的荷塘,在這滿月的光裡,總該另有一番樣子吧。月亮漸漸地公升高了,牆外馬路上孩子們的歡笑,已經聽不見了 妻在屋裡拍著閏兒,迷迷糊糊地哼著眠歌。我悄悄地披了大衫,帶上門出去。沿著荷塘,是一條曲折的小煤屑路。這是一條幽...
SQL Server 查詢效能優化 相關文章
來自 sql server 查詢效能優化 堆表 碎片與索引 一 sql server 查詢效能優化 堆表 碎片與索引 二 sql server 查詢效能優化 覆蓋索引 一 sql server 查詢效能優化 覆蓋索引 二 sql server 查詢效能優化 建立索引原則 一 sql server 查...
SQL Server 查詢效能優化 相關文章
來自 sql server 查詢效能優化 堆表 碎片與索引 一 sql server 查詢效能優化 堆表 碎片與索引 二 sql server 查詢效能優化 覆蓋索引 一 sql server 查詢效能優化 覆蓋索引 二 sql server 查詢效能優化 建立索引原則 一 sql server 查...