同一个数据库里有表一和表二,记录如下: 
表一(菜类): 
ID  ming      c_name  color    time 
96  张学友    大白菜      白色  2008-10-16 
95  刘德华    罗卜        红色  2008-10-12 
94  陈忠和    花菜        黄色  2008-10-10 
93  刘德华    青椒        绿色  2008-10-8 
92  李明兰    扁豆        绿色  2008-10-3 表二(肉类): 
ID  ming      r_name  zhong    time 
32  张学友    猪肉        20  2008-10-17 
31  李明一    鸡肉    23  2008-10-15 
30  陈忠和    鸭肉    10  2008-10-13 
29  刘德华    牛肉     5    2008-10-14 
28  李明兰    鸡肉     3    2008-10-7 ===================================== 
表一和表二都可能出现多次同一姓名的记录,要求调用时按两个表的时间从大到小排序,同一姓名的只出现一次,显示效果如下: 张学友 
猪肉|大白菜 李明一 
鸡肉 刘德华 
牛肉|罗卜|青椒 陈忠和 
鸭肉|花菜 李明兰 
鸡肉|扁豆 ============================ 
不改变数据库结构,怎样实现以上的调用显示效果,如果我描述不清楚请提问。 

解决方案 »

  1.   


    create function str_f (@name nvarchar(10))
    returns nvarchar(4000)
    as
    declare @str nvarchar(4000)
    select @str='|'+c_name from T where ming=@name
    set @str=stuff(@str,1,1,'')
    return @strwith T as(ming,c_name)
    ( select ming ,c_name,time from 表1
      union all
      select ming,r_name
    )
    select ming ,c_name=strf(ming) from T group by time
      

  2.   


    create function str_f (@name nvarchar(10))
    returns nvarchar(4000)
    as
    declare @str nvarchar(4000)
    select @str='|'+c_name from T where ming=@name
    set @str=stuff(@str,1,1,'')
    return @strwith T as(ming,c_name)
    ( select ming ,c_name,time from 表1
      union all
      select ming,r_name
    )
    select ming ,c_name=strf(ming) from T group by time
      

  3.   


    create function str_f (@name nvarchar(10))
    returns nvarchar(4000)
    as
    declare @str nvarchar(4000)
    select @str='|'+c_name from T where ming=@name
    set @str=stuff(@str,1,1,'')
    return @strwith T as(ming,c_name)
    ( select ming ,c_name,time from 表1
      union all
      select ming,r_name,time from 表2
    )
    select ming ,c_name=strf(ming) from T group by time desc
      

  4.   


    create function str_f (@name nvarchar(10))
    returns nvarchar(4000)
    as
    declare @str nvarchar(4000)
    select @str= @str+'|'+c_name from T where ming=@name
    set @str=stuff(@str,1,1,'')
    return @strwith T as(ming,c_name)
    ( select ming ,c_name,time from 表1
      union all
      select ming,r_name,time from 表2
    )
    select ming ,c_name=strf(ming) from T group by time desc
      

  5.   


    create function str_f (@name nvarchar(10))
    returns nvarchar(4000)
    as
    declare @str nvarchar(4000)
    select @str= @str+'|'+c_name from T where ming=@name
    set @str=stuff(@str,1,1,'')
    return @strwith T as(ming,c_name)
    ( select ming ,c_name,time from 表1
      union all
      select ming,r_name,time from 表2
    )
    select ming ,c_name=strf(ming) from T group by ming  order by time desc  
      

  6.   

    这些是Sql语句吗,我也不太懂,试了一下,显示错误:Microsoft JET Database Engine (0x80040E14)
    CREATE TABLE 语句中的语法错误。
      

  7.   

    对了,我是做asp + access
      

  8.   

    create table tb1 (ID int,ming varchar(40),c_name varchar(40),color varchar(20),ttime datetime)
    insert into tb1
    select 96,'张学友','大白菜','色','2008-10-16' union all
    select 95,'刘德华','罗卜','红色','2008-10-12 ' union all
    select 94,'陈忠和','花菜','黄色','2008-10-10 ' union all
    select 93,'刘德华','青椒','绿色','2008-10-8' union all
    select 92,'李明兰','扁豆','绿色','2008-10-3'
    create table tb2 (ID int,ming varchar(40),r_name varchar(40),zhong int,ttime datetime)
    insert into tb2 
    select 32,'张学友','猪肉',20,'2008-10-17' union all
    select 31,'李明一','鸡肉',23,'2008-10-15' union all
    select 30,'陈忠和','鸭肉',10,'2008-10-13' union all
    select 29,'刘德华','牛肉',5,'2008-10-14' union all
    select 28,'李明兰','鸡肉',3,'2008-10-7'
    go
    create function SumStr(@ifstr varchar(100)) returns varchar(1000)
     as 
    begin
          declare @nowstr varchar(1000)
          set @nowstr=''
          select @nowstr=@nowstr + a.c_name + '|' from (select c_name,ttime from tb1 where ming=@ifstr union all select r_name,ttime from tb2 where ming=@ifstr) a order by a.ttime desc
          return substring(@nowstr,1,len(@nowstr)-1)
    end
    goselect a.ming,sumname=dbo.SumStr(a.ming) from (select ming,ttime from tb1 union all select ming,ttime from tb2) a  group by a.ming order by max(ttime) desc
    if object_id('tb1') is not null drop table tb1
    if object_id('tb2') is not null drop table tb2
    drop function SumStr--结果
    (所影响的行数为 5 行)
    (所影响的行数为 5 行)                                                           
     ming                                    sumname 
    张学友                                      猪肉|大白菜
    李明一                                      鸡肉
    刘德华                                      牛肉|罗卜|青椒
    陈忠和                                      鸭肉|花菜
    李明兰                                      鸡肉|扁豆(所影响的行数为 5 行)
      

  9.   

    create table tb1 (ID int,ming varchar(40),c_name varchar(40),color varchar(20),ttime datetime)
    insert into tb1
    select 96,'张学友','大白菜','色','2008-10-16' union all
    select 95,'刘德华','罗卜','红色','2008-10-12 ' union all
    select 94,'陈忠和','花菜','黄色','2008-10-10 ' union all
    select 93,'刘德华','青椒','绿色','2008-10-8' union all
    select 92,'李明兰','扁豆','绿色','2008-10-3'
    create table tb2 (ID int,ming varchar(40),r_name varchar(40),zhong int,ttime datetime)
    insert into tb2 
    select 32,'张学友','猪肉',20,'2008-10-17' union all
    select 31,'李明一','鸡肉',23,'2008-10-15' union all
    select 30,'陈忠和','鸭肉',10,'2008-10-13' union all
    select 29,'刘德华','牛肉',5,'2008-10-14' union all
    select 28,'李明兰','鸡肉',3,'2008-10-7'
    go
    create function SumStr(@ifstr varchar(100)) returns varchar(1000)
     as 
    begin
          declare @nowstr varchar(1000)
          set @nowstr=''
          select @nowstr=@nowstr + a.c_name + '|' from (select c_name,ttime from tb1 where ming=@ifstr union all select r_name,ttime from tb2 where ming=@ifstr) a order by a.ttime desc
          return substring(@nowstr,1,len(@nowstr)-1)
    end
    goselect a.ming,sumname=dbo.SumStr(a.ming) from (select ming,ttime from tb1 union all select ming,ttime from tb2) a  group by a.ming order by max(ttime) desc
    if object_id('tb1') is not null drop table tb1
    if object_id('tb2') is not null drop table tb2
    drop function SumStr