表中字段contents存储的是文章,我需要用语句识别出来,这篇文章是属于那个地区的。其中info表是文章表,diqu表是存储地区的表。用这个下面的语句能实现要的效果,但是不是太精确。因为文章字段contents可能含有多个地区。
我想可以这样,取那个地区出现次数最多的为其地区,或者有多个地区的就按全国处理?大家看这个语句怎么写。
各位高手看看。select t.contents,d.dq
from info t
join diqu d on charindex(d.dq,convert(varchar(8000),t.contents)) >0  and intime>'2007-8-24' order by title--------------------
表diqu
21辽宁
22吉林
23黑龙江
31上海
32江苏
33浙江
34安徽
35福建
36江西表info
title 广西调整企业退休人员基本养老金 
contents 8月27日,记者从广西自治区劳动和社会保障厅获悉,我区将在一定范围内调整企业退休人员基本养老金,本次调整时间从7月1日开始。     按照规定,参加本次调整基本养老金的人员包括三类:一是2006年12月31日前已经按规定办理退休(退职)手续,并已经按月领取基本养老金的企业退休(退职)人员。二是已经参加了城镇企业职工基本养老保险,并在2006年12月31日前已经按规定办理退休(退职)手续,且已经按月领取基本养老金的企业化管理事业单位的退休(退职)人员。三是已经参加了城镇企业职工基本养老保险,并在2006年1

解决方案 »

  1.   

    --出现次数
    (len(contents)-len(REPLACE(contents,dq)))/len(dq)
      

  2.   

    (len(contents)-len(REPLACE(contents,dq,'')))/len(dq)
      

  3.   

    --不知道我理解错了没
    create table info(
    title varchar(8000)) 
    insert into info select '广西调整企业退休人员基本养老金 
    contents 8月27日,记者从广西自治区劳动和社会保障厅获悉,
    我区将在一定范围内调整企业退休人员基本养老金,本次调整时间从7月1日开始。     
    按照规定,参加本次调整基本养老金的人员包括三类:一是2006年12月31日前已经按规定办理退休(退职)手续,
    并已经按月领取基本养老金的企业退休(退职)人员。二是已经参加了城镇企业职工基本养老保险,
    并在2006年12月31日前已经按规定办理退休(退职)手续,且已经按月领取基本养老金的企业化管理事业单位
    的退休(退职)人员。三是已经参加了城镇企业职工基本养老保险,并在2006年1'create table diqu
    (id int,dq varchar(20))
    insert into diqu select 21,'辽宁'
    union all select 22,'吉林'
    union all select 23,'黑龙江'
    union all select 31,'上海'
    union all select 32,'江苏'
    union all select 33,'浙江'
    union all select 34,'安徽'
    union all select 35,'福建'
    union all select 36,'江西'
    insert into diqu select 1,'广西'select a.*,(len(title)-len(REPLACE(title,dq,'')))/len(dq)[count]
    from diqu a join info b
    on charindex(dq,title)>0id          dq                   count       
    ----------- -------------------- ----------- 
    1           广西                   2(所影响的行数为 1 行)
      

  4.   

    广东,2
    广西,2
    内蒙古,3
    黑龙江,3中国最多字的省份为3select t.contents,省份 = '某省'
    from info t,diqu d 
    where len(t.contents) - len(replace(t.contents,d.dq,''))<=3 and intime>'2007-8-24' 
    union all
    select t.contents,省份 = '全国'
    from info t,diqu d 
    where len(t.contents) - len(replace(t.contents,d.dq,''))>3 and intime>'2007-8-24' 此法不一定可取
      

  5.   

    应该不对,应该是当contents字段中有多个地区出现的时候,要比较出一个出现次数最多的地区。
      

  6.   

    或者条件改为,contents中第一次出现的地区。
    select t.contents,d.dq
    from info t
    join diqu d on charindex(d.dq,convert(varchar(8000),t.contents)) >0  and intime>'2007-8-24' order by title因为现在这样,只要diqu表中有它都会查询出来一次。
      

  7.   

    dawugui(潇洒老乌龟) 不懂你的意思~是和我讲吗?
    bluesun() 什么不对?
      

  8.   

    --不好意思title和contents写一起了~让你们误会了! 
    --dawugui(潇洒老乌龟)是出现两次不是两个字符!!!
    create table info
    (
    title varchar(100),
    contents varchar(7000)

    insert into info select '广西调整企业退休人员基本养老金'
    ,'8月27日,记者从广西自治区劳动和社会保障厅获悉,
    我区将在一定范围内调整企业退休人员基本养老金,本次调整时间从7月1日开始。     
    按照规定,参加本次调整基本养老金的人员包括三类:一是2006年12月31日前已经按规定办理退休(退职)手续,
    并已经按月领取基本养老金的企业退休(退职)人员。二是已经参加了城镇企业职工基本养老保险,
    并在2006年12月31日前已经按规定办理退休(退职)手续,且已经按月领取基本养老金的企业化管理事业单位
    的退休(退职)人员。三是已经参加了城镇企业职工基本养老保险,并在2006年1'create table diqu
    (id int,dq varchar(20))
    insert into diqu select 21,'辽宁'
    union all select 22,'吉林'
    union all select 23,'黑龙江'
    union all select 31,'上海'
    union all select 32,'江苏'
    union all select 33,'浙江'
    union all select 34,'安徽'
    union all select 35,'福建'
    union all select 36,'江西'
    insert into diqu select 1,'广西'select *,
    (select top 1 dq
    from diqu b where charindex(dq,contents)>0
    order by (len(contents)-len(REPLACE(contents,dq,'')))/len(dq) desc)[地名]
    from info a
      

  9.   

    (len(contents)-len(REPLACE(contents,dq,'')))/len(dq)
    学习了 
    ... 谢谢砍破...
      

  10.   

    --dawugui(潇洒老乌龟)是出现两次不是两个字符!!!一个汉字占一位.意思是如果被替换了四个字以上应该至少有两个地区.因为中国的省份最多的只有三个字.
    黑龙江,内蒙古
      

  11.   

    --dawugui(潇洒老乌龟)
    (len(contents)-len(REPLACE(contents,内蒙古,'')))/len(内蒙古)
      

  12.   

    print(len('黑龙江,内蒙古')-len(REPLACE('黑龙江,内蒙古','内蒙古','')))/len('内蒙古')
    ---------
    1
      

  13.   

    --order by地名出现次数 desc,出现位置(先后)
    select *,
    (select top 1 dq
    from diqu b where charindex(dq,contents)>0
    order by (len(contents)-len(REPLACE(contents,dq,'')))/len(dq) desc,charindex(dq,contents))[地名]
    from info a
      

  14.   

    to:w75251455(砍破)
    但contents是text字段哦
      

  15.   

    转啊!!cast(col as varchar(8000))
    如果超过8000我就没招啦