表1名:home :  CID (主键),name       playid
                1            黄山         1,
                2            武夷山       4,
                3            峨眉山       3,
                4            长城         1,
                5            长泰漂流     3,
                6            长青         2,
                7            昆仑山       1,
  表2名:homeurl : id (主键),homeid , url 
                  1            1      500公里 
                  2            2      190公里 
                  3            5      684公里 
                  4            3      221公里 
                  5            6      245公里 
                  6            7      544公里 表3名:PLAY  cid(主键) name
              1        一等级
              2        特别好
              3        三星
              4        最新打造
        
求表1 name含有山的值并找出表名对于的URL 
如:NAME含山的数据 cid  name  ID  URL        name(play表)
1  :黄山   1   500公里    一等级  
2  :武夷山 2   190公里    最新打造
3  :峨眉山 4   221公里    三星
..........刚才瞬间被小F解答了,现在加强难度了,同时注明:playid 不是INT格式,且数据里有逗号,字符~解答吧!
 

解决方案 »

  1.   

    --函数
    Create Function dbo.test_f(@name  varchar(200))
    Returns Table
    As
    BEGIN
    Return ('select a.cid,a.name,b.id,b.url from home a
    left join homeurl b
    on a.cid=b.homeid
    left join play c
    on  a.cid=c.cid
    and a.name like ''%'+@name+'%''')
    END--调用
    select dbo.test_f(山)
      

  2.   

    少个字段,加上
    --函数
    Create Function dbo.test_f(@name  varchar(200))
    Returns Table
    As
    BEGIN
    Return ('select a.cid,a.name,b.id,b.url,c.name from home a
    left join homeurl b
    on a.cid=b.homeid
    left join play c
    on  a.cid=c.cid
    and a.name like ''%'+@name+'%''')
    END--调用
    select dbo.test_f(山)
      

  3.   

    如果不需要函数,直接这样select a.cid,a.name,b.id,b.url from home a
    left join homeurl b
    on a.cid=b.homeid
    left join play c
    on  a.cid=c.cid
    and a.name like '%山%'
      

  4.   


    select a.cid,a.name,b.id,b.url
    from home a join homeurl b
    ob a.CID = b.homeid join PLAY c
    on convert(int,substring(a.playid,1,charindex(',',a.playid1)-1)) = c.cid
    where charindex('山',a.name,1) > 0 
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-10-25 15:59:01
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[home]
    if object_id('[home]') is not null drop table [home]
    go 
    create table [home]([CID] int,[name] varchar(8),[playid] varchar(2))
    insert [home]
    select 1,'黄山','1,' union all
    select 2,'武夷山','4,' union all
    select 3,'峨眉山','3,' union all
    select 4,'长城','1,' union all
    select 5,'长泰漂流','3,' union all
    select 6,'长青','2,' union all
    select 7,'昆仑山','1,'
    --> 测试数据:[homeurl]
    if object_id('[homeurl]') is not null drop table [homeurl]
    go 
    create table [homeurl]([id] int,[homeid] int,[url] varchar(7))
    insert [homeurl]
    select 1,1,'500公里' union all
    select 2,2,'190公里' union all
    select 3,5,'684公里' union all
    select 4,3,'221公里' union all
    select 5,6,'245公里' union all
    select 6,7,'544公里'
    --> 测试数据:[PLAY]
    if object_id('[PLAY]') is not null drop table [PLAY]
    go 
    create table [PLAY]([cid] int,[name] varchar(8))
    insert [PLAY]
    select 1,'一等级' union all
    select 2,'特别好' union all
    select 3,'三星' union all
    select 4,'最新打造'
    --------------开始查询--------------------------
    select 
      a.cid,a.name,left(playid,1) as playid,b.id,b.url 
    from 
      [home] a 
    join  
      [homeurl] b 
    on 
      a.CID=b.homeid 
    join
      [PLAY] c
    on 
      left(a.playid,1)=c.cid
    where 
      a.name like '%山'
    ----------------结果----------------------------
    /* cid         name     playid id          url
    ----------- -------- ------ ----------- -------
    1           黄山       1      1           500公里
    2           武夷山      4      2           190公里
    3           峨眉山      3      4           221公里
    7           昆仑山      1      6           544公里(4 行受影响)*/
      

  6.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-10-25 15:59:01
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[home]
    if object_id('[home]') is not null drop table [home]
    go 
    create table [home]([CID] int,[name] varchar(8),[playid] varchar(2))
    insert [home]
    select 1,'黄山','1,' union all
    select 2,'武夷山','4,' union all
    select 3,'峨眉山','3,' union all
    select 4,'长城','1,' union all
    select 5,'长泰漂流','3,' union all
    select 6,'长青','2,' union all
    select 7,'昆仑山','1,'
    --> 测试数据:[homeurl]
    if object_id('[homeurl]') is not null drop table [homeurl]
    go 
    create table [homeurl]([id] int,[homeid] int,[url] varchar(7))
    insert [homeurl]
    select 1,1,'500公里' union all
    select 2,2,'190公里' union all
    select 3,5,'684公里' union all
    select 4,3,'221公里' union all
    select 5,6,'245公里' union all
    select 6,7,'544公里'
    --> 测试数据:[PLAY]
    if object_id('[PLAY]') is not null drop table [PLAY]
    go 
    create table [PLAY]([cid] int,[name] varchar(8))
    insert [PLAY]
    select 1,'一等级' union all
    select 2,'特别好' union all
    select 3,'三星' union all
    select 4,'最新打造'
    --------------开始查询--------------------------
    select 
      a.cid,a.name,left(playid,1) as playid,b.id,b.url,c.name
    from 
      [home] a 
    join  
      [homeurl] b 
    on 
      a.CID=b.homeid 
    join
      [PLAY] c
    on 
      left(a.playid,1)=c.cid
    where 
      a.name like '%山'
    ----------------结果----------------------------
    /*cid         name     playid id          url     name
    ----------- -------- ------ ----------- ------- --------
    1           黄山       1      1           500公里   一等级
    2           武夷山      4      2           190公里   最新打造
    3           峨眉山      3      4           221公里   三星
    7           昆仑山      1      6           544公里   一等级(4 行受影响)*/
      

  7.   

    ---这样准确些
    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-10-25 15:59:01
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[home]
    if object_id('[home]') is not null drop table [home]
    go 
    create table [home]([CID] int,[name] varchar(8),[playid] varchar(2))
    insert [home]
    select 1,'黄山','1,' union all
    select 2,'武夷山','4,' union all
    select 3,'峨眉山','3,' union all
    select 4,'长城','1,' union all
    select 5,'长泰漂流','3,' union all
    select 6,'长青','2,' union all
    select 7,'昆仑山','1,'
    --> 测试数据:[homeurl]
    if object_id('[homeurl]') is not null drop table [homeurl]
    go 
    create table [homeurl]([id] int,[homeid] int,[url] varchar(7))
    insert [homeurl]
    select 1,1,'500公里' union all
    select 2,2,'190公里' union all
    select 3,5,'684公里' union all
    select 4,3,'221公里' union all
    select 5,6,'245公里' union all
    select 6,7,'544公里'
    --> 测试数据:[PLAY]
    if object_id('[PLAY]') is not null drop table [PLAY]
    go 
    create table [PLAY]([cid] int,[name] varchar(8))
    insert [PLAY]
    select 1,'一等级' union all
    select 2,'特别好' union all
    select 3,'三星' union all
    select 4,'最新打造'
    --------------开始查询--------------------------
    select 
      a.cid,a.name,b.id,b.url,c.name
    from 
      [home] a 
    join  
      [homeurl] b 
    on 
      a.CID=b.homeid 
    join
      [PLAY] c
    on 
      convert(int,substring(a.playid,1,charindex(',',a.playid)-1)) =c.cid
    where 
      a.name like '%山'
    ----------------结果----------------------------
    /*cid         name     id          url     name
    ----------- -------- ----------- ------- --------
    1           黄山       1           500公里   一等级
    2           武夷山      2           190公里   最新打造
    3           峨眉山      4           221公里   三星
    7           昆仑山      6           544公里   一等级(4 行受影响)*/
      

  8.   


    服务器: 消息 170,级别 15,状态 31,过程 test_f,行 11
    第 11 行: 'BEGIN' 附近有语法错误。我的是SQL2000!
      

  9.   

    playid 不是INT格式,且数据里有逗号,字符~ 
    你要是后面有多个那才多了点东西,多了个逗号,后面没数据可以用REPLACE(PLAYID,',','')
      

  10.   


    在SQL平台上也可以用REPLACE? 
      

  11.   

    create table [home]([CID] int,[name] varchar(8),[playid] varchar(2))
    insert [home]
    select 1,'黄山','1,' union all
    select 2,'武夷山','4,' union all
    select 3,'峨眉山','3,' union all
    select 4,'长城','1,' union all
    select 5,'长泰漂流','3,' union all
    select 6,'长青','2,' union all
    select 7,'昆仑山','1,'
    go 
    create table [homeurl]([id] int,[homeid] int,[url] varchar(7))
    insert [homeurl]
    select 1,1,'500公里' union all
    select 2,2,'190公里' union all
    select 3,5,'684公里' union all
    select 4,3,'221公里' union all
    select 5,6,'245公里' union all
    select 6,7,'544公里'
    create table [PLAY]([cid] int,[name] varchar(8))
    insert [PLAY]
    select 1,'一等级' union all
    select 2,'特别好' union all
    select 3,'三星' union all
    select 4,'最新打造'select t1.cid , t1.name , t2.id , t2.url , t3.name
    from home t1,homeurl t2,PLAY t3
    where t1.playid = cast(t2.homeid as varchar) + ',' and t2.homeid = t3.cid and t1.name like '%山%'drop table home,homeurl,PLAY/*
    cid         name     id          url     name     
    ----------- -------- ----------- ------- -------- 
    1           黄山       1           500公里   一等级
    7           昆仑山      1           500公里   一等级
    3           峨眉山      4           221公里   三星(所影响的行数为 3 行)*/
      

  12.   


    select a.cid,a.name,b.id,b.url,c.name
    from home a join homeurl b
    on a.CID = b.homeid join PLAY c
    on convert(int,substring(a.playid,1,charindex(',',a.playid)-1)) = c.cid
    where charindex('山',a.name,1) > 0 
      

  13.   

    如果只有一个',',而且是以','结尾,条件可以用convert(int,REPLACE(a.playid,',',''))=c.cid
    否则就用上面的
      

  14.   

    create table [home]([CID] int,[name] varchar(8),[playid] varchar(2))
    insert [home]
    select 1,'黄山','1,' union all
    select 2,'武夷山','4,' union all
    select 3,'峨眉山','3,' union all
    select 4,'长城','1,' union all
    select 5,'长泰漂流','3,' union all
    select 6,'长青','2,' union all
    select 7,'昆仑山','1,'
    go 
    create table [homeurl]([id] int,[homeid] int,[url] varchar(7))
    insert [homeurl]
    select 1,1,'500公里' union all
    select 2,2,'190公里' union all
    select 3,5,'684公里' union all
    select 4,3,'221公里' union all
    select 5,6,'245公里' union all
    select 6,7,'544公里'
    create table [PLAY]([cid] int,[name] varchar(8))
    insert [PLAY]
    select 1,'一等级' union all
    select 2,'特别好' union all
    select 3,'三星' union all
    select 4,'最新打造'select t1.cid , t1.name , t2.id , t2.url , t3.name
    from home t1,homeurl t2,PLAY t3
    where t1.cid = t2.id and t1.playid = cast(t3.cid as varchar) + ',' and t1.name like '%山%'drop table home,homeurl,PLAY/*
    cid         name     id          url     name     
    ----------- -------- ----------- ------- -------- 
    1           黄山       1           500公里   一等级
    2           武夷山      2           190公里   最新打造
    3           峨眉山      3           684公里   三星(所影响的行数为 3 行)*/