--数据如下:
mobile            daytime         content
13733085588 2011-8-25 13:17:08 点一首歌特别送给蓝水晶 藏古西烈<合不来分不开>
13909607914 2011-8-25 13:15:08 男
13909607914 2011-8-25 13:14:39 我想交个女性在25左右想和她交个朋友
15556335003 2011-8-25 13:12:07 主持人你好我想交友交18-20岁的女孩为友
13485500520 2011-8-25 13:07:14 主持人你好!
13485500520 2011-8-25 12:47:58 祝愿我的朋友小雨开心快乐每一天!
15156597052 2011-8-25 12:43:10 不要沉陷于以往感情的伤痛,要走出去。
18756638785 2011-8-25 12:42:41 蛋蛋,张杰的天下祝雁楠幸福了。--希望得到结果:
查询content中包含“交”或者“祝”或“不”或“了”的,如果不存在则取除了这个要求外的content(不过不包括仅仅一个“男”字),如果也不存在,就取content为“男”的
每个mobile仅取1条daytime最新的符合上面这个条件的content--脚本如下
if object_id('tb','u') is not null
drop table tb
gocreate table tb(
mobile varchar(20),
    daytime datetime,
    content varchar(100)
)
goinsert into tb(mobile,daytime,content)
select '13733085588','2011-8-25 13:17:08','点一首歌特别送给蓝水晶 藏古西烈<合不来分不开>'
union all select '13909607914','2011-8-25 13:15:08','男'
union all select '13909607914','2011-8-25 13:14:39','我想交个女性在25左右想和她交个朋友'
union all select '15556335003','2011-8-25 13:12:07','主持人你好我想交友交18-20岁的女孩为友'
union all select '13485500520','2011-8-25 13:07:14','主持人你好!'
union all select '13485500520','2011-8-25 12:47:58','祝愿我的朋友小雨开心快乐每一天!'
union all select '15156597052','2011-8-25 12:43:10','不要沉陷于以往感情的伤痛,要走出去。'
union all select '18756638785','2011-8-25 12:42:41','蛋蛋,张杰的天下祝雁楠幸福了。'
goselect * from tb
godrop table tb
go

解决方案 »

  1.   

    if object_id('tb','u') is not null
        drop table tb
    go
    create table tb(
        mobile varchar(20),
        daytime datetime,
        content varchar(100)
    )
    goinsert into tb(mobile,daytime,content)
    select '13733085588','2011-8-25 13:17:08','点一首歌特别送给蓝水晶 藏古西烈<合不来分不开>'
    union all select '13909607914','2011-8-25 13:15:08','男'
    union all select '13909607914','2011-8-25 13:14:39','我想交个女性在25左右想和她交个朋友'
    union all select '15556335003','2011-8-25 13:12:07','主持人你好我想交友交18-20岁的女孩为友'
    union all select '13485500520','2011-8-25 13:07:14','主持人你好!'
    union all select '13485500520','2011-8-25 12:47:58','祝愿我的朋友小雨开心快乐每一天!'
    union all select '15156597052','2011-8-25 12:43:10','不要沉陷于以往感情的伤痛,要走出去。'
    union all select '18756638785','2011-8-25 12:42:41','蛋蛋,张杰的天下祝雁楠幸福了。'
    go
    select * from tb a
     where patindex('%[交祝不了]%',content)>0
     and daytime=(select MAX(daytime) from tb 
         where mobile=a.mobile and patindex('%[交祝不了]%',content)>0)
         /*
    mobile               daytime                 content
    -------------------- ----------------------- ---------------------------------------------------
    18756638785          2011-08-25 12:42:41.000 蛋蛋,张杰的天下祝雁楠幸福了。
    15556335003          2011-08-25 13:12:07.000 主持人你好我想交友交18-20岁的女孩为友
    15156597052          2011-08-25 12:43:10.000 不要沉陷于以往感情的伤痛,要走出去。
    13909607914          2011-08-25 13:14:39.000 我想交个女性在25左右想和她交个朋友
    13733085588          2011-08-25 13:17:08.000 点一首歌特别送给蓝水晶 藏古西烈<合不来分不开>
    13485500520          2011-08-25 12:47:58.000 祝愿我的朋友小雨开心快乐每一天!(6 行受影响)     
      

  2.   


    --一个where条件就可以了
    select * from tb a
     where daytime=(select MAX(daytime) from tb 
         where mobile=a.mobile and patindex('%[交祝不了]%',content)>0)
      

  3.   

    威武,patindex正则竟然忘了,杯具!!!
      

  4.   

    select
       distinct b.*
    from
       tb a
    cross apply
       (select top 1 * from tb where mobile=a.mobile and patindex('%[交祝不了]%',content)>0 order by daytime desc)b
      

  5.   

    --脚本如下
    if object_id('tb','u') is not null
        drop table tb
    gocreate table tb(
        mobile varchar(20),
        daytime datetime,
        content varchar(100)
    )
    goinsert into tb(mobile,daytime,content)
    select '13733085588','2011-8-25 13:17:08','点一首歌特别送给蓝水晶 藏古西烈<合不来分不开>'
    union all select '13909607914','2011-8-25 13:15:08','男'
    union all select '13909607914','2011-8-25 13:14:39','我想交个女性在25左右想和她交个朋友'
    union all select '15556335003','2011-8-25 13:12:07','主持人你好我想交友交18-20岁的女孩为友'
    union all select '13485500520','2011-8-25 13:07:14','主持人你好!'
    union all select '13485500520','2011-8-25 12:47:58','祝愿我的朋友小雨开心快乐每一天!'
    union all select '15156597052','2011-8-25 12:43:10','不要沉陷于以往感情的伤痛,要走出去。'
    union all select '18756638785','2011-8-25 12:42:41','蛋蛋,张杰的天下祝雁楠幸福了。'
    goselect
       distinct b.*
    from
       tb a
    cross apply
       (select top 1 * from tb where mobile=a.mobile and patindex('%[交祝不了]%',content)>0 order by daytime desc)b
       
       /*mobile               daytime                 content
    -------------------- ----------------------- ----------------------------------------------------------------------------------------------------
    13485500520          2011-08-25 12:47:58.000 祝愿我的朋友小雨开心快乐每一天!
    13733085588          2011-08-25 13:17:08.000 点一首歌特别送给蓝水晶 藏古西烈<合不来分不开>
    13909607914          2011-08-25 13:14:39.000 我想交个女性在25左右想和她交个朋友
    15156597052          2011-08-25 12:43:10.000 不要沉陷于以往感情的伤痛,要走出去。
    15556335003          2011-08-25 13:12:07.000 主持人你好我想交友交18-20岁的女孩为友
    18756638785          2011-08-25 12:42:41.000 蛋蛋,张杰的天下祝雁楠幸福了。(6 行受影响)*/