sql查询问题数据设计如下books表书籍表,每本书的有很多文章,保存在articles字段中articles表包为文章

1个回答

  • create table books(id int primary key,articles varchar(100))

    insert into books values(1,'1,2,3')

    insert into books values(2,'4,5,6')

    --select * from books

    create table articles(id int primary key,authors varchar(100))

    insert into articles values(1,'1,2,3')

    insert into articles values(2,'4,5,6')

    insert into articles values(3,'4,5,6')

    insert into articles values(4,'4,5,6')

    insert into articles values(5,'1,2,3')

    insert into articles values(6,'1,2,3')

    --select * from articles

    create table author(id int primary key,authorName varchar(100))

    insert into author values(1,'张三')

    insert into author values(2,'李四')

    insert into author values(3,'王五')

    insert into author values(4,'小麦')

    insert into author values(5,'小王')

    insert into author values(6,'小李')

    --select * from author

    --drop function MySql1

    create function MySql1(@id int)

    returns varchar(8000)

    as

    begin

    declare @len int,@ar varchar(100),@sql varchar(8000)

    set @sql='select distinct authors from articles where '

    select @ar=articles from books where id=1

    select @len=len(@ar)

    while(@len>0)

    begin

    if(left(@ar,1)',')

    begin

    select @sql=@sql+'id='+left(@ar,1)+' '

    if(@len>1)

    select @sql=@sql+'or '

    end

    set @len=@len-1

    set @ar=substring(@ar,2,@len)

    end

    return (@sql)

    end

    --select dbo.MySql1(1)

    --exec sp_helptext Mysql1

    --drop proc Myprc

    create table MyetempTB(chr varchar(100))

    create proc Myprc(@Myii int)

    as

    begin

    declare @sql varchar(8000) set @sql=dbo.Mysql1(@Myii)

    exec (@sql)

    end

    go

    --drop proc Myprc2

    create proc Myprc2(@Myii int)

    as

    begin

    delete from MyetempTB

    insert into MyetempTB exec dbo.Myprc @Myii

    declare @ret varchar(8000)

    set @ret = ''

    select @ret = @ret+','+chr from MyetempTB

    set @ret = stuff(@ret,1,1,'')

    declare @sql varchar(8000)

    set @sql='select distinct * from author where '

    declare @len int set @len=len(@ret)

    while(@len>0)

    begin

    if(left(@ret,1)',')

    begin

    select @sql=@sql+'id='+left(@ret,1)+' '

    if(@len>1)

    select @sql=@sql+'or '

    end

    set @len=@len-1

    set @ret=substring(@ret,2,@len)

    end

    exec (@sql)

    end

    go

    执行:

    exec Myprc2 1

    测试结果如下:

    (所影响的行数为 2 行)

    (所影响的行数为 2 行)

    id authorName

    ----------- ----------------------------------------------------------------------------------------------------

    1 张三

    2 李四

    3 王五

    4 小麦

    5 小王

    6 小李

    (所影响的行数为 6 行)

    --基本逻辑如下:

    --首先查询出books表中的articles

    --MySql1函数:

    --它是将articles查询出的结果拼凑成一条查询语句,此条查询语句拼凑后基本如下:

    --'select distinct authors from articles where id=1 or id=2 or id=3'

    --当然如果查询出的结果是7,8,9 那拼凑后的语句就是:

    --'select distinct authors from articles where id=7 or id=8 or id=9'

    --create table MyetempTB(chr varchar(100))是建立一个转储表,这个表是不可缺少的

    --存储过程 Myprc(@Myii int)是将拼凑的语句查询出表

    --存储过程Myprc2函数主要步骤:

    --将Myprc查询出的表利用临时转储表储存,在将转储表的authors字段内的所有内容组合起来拼凑成查询语句

    --转储表的内容是两行数据:

    --第一行: 1,2,3

    --第二行: 4,5,6

    --内容组合: 1,2,3,4,5,6

    --查询语句拼凑后如下:

    --'select distinct * from author where id=1 or id=2 or id=3 or id=4 or id=5 or id=6'

    --执行Myprc2 参数是你要查询的ID

    --执行结果当然是楼主你想要的数据了

    --楼主可以多插入一些测试数据,测试一下是否正确