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

求一SQL语句...急.......
现有两张表,

create table table1
(
  KEYWORD VARCHAR2(1000),//关键词
  HOLDDAY VARCHAR2(2), //保留天数
  PRIORITY VARCHAR2(2) //优前级(数字)
)

create table table2
(
  TITLE VARCHAR2(1000),//标题
  CONTENT VARCHAR2(2), //正文
  CRTDATE DATE //创建时间
)
需要实现功能:
遍历table2,查找table2记录中标题和正文是否包含table1(table1中有多条记录,需要遍历)中关键词的记录,并按优先级进行排序(如果table2记录的创建时间与系统时间相比超过优先级保留天数则该记录无优先级)
通过SQL语句实现,不要存储过程啥的。。。

------解决方案--------------------
SQL code
select * from table2 a left join table1 b
on charindex(a.KEYWORD,b.TITLE )>0
order by a.CRTDATE-getdate()

------解决方案--------------------
SQL code
select m.title, m.content, m.crtdate, n.keyword, n.holdday,
    priority = case when datediff(day, getdate(), n.CRTDATE) > n.holdday then '' else n.priority end
from
(select * from table2) m
cross apply
(
    select top(1) *
    from table1 a
        inner join table2 b
            on b.title like '%'+a.keyword+'%' or b.content like '%'+a.keyword+'%'
    where b.title = m.title and b.content = m.coneten and b.crtdate = m.crtdate
    order by a.priority
) n

------解决方案--------------------
探讨
SQL code
select m.title, m.content, m.crtdate, n.keyword, n.holdday,
priority = case when datediff(day, getdate(), n.CRTDATE) > n.holdday then '' else n.priority end
from
(select * from table2……