同一个数据库里有表一和表二,记录如下:
表一(菜类):
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 =====================================
表一和表二都可能出现多次同一姓名的记录,要求调用时按两个表的时间从大到小排序,同一姓名的只出现一次,显示效果如下: 张学友
猪肉|大白菜 李明一
鸡肉 刘德华
牛肉|罗卜|青椒 陈忠和
鸭肉|花菜 李明兰
鸡肉|扁豆 ============================
不改变数据库结构,怎样实现以上的调用显示效果,如果我描述不清楚请提问。
表一(菜类):
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 =====================================
表一和表二都可能出现多次同一姓名的记录,要求调用时按两个表的时间从大到小排序,同一姓名的只出现一次,显示效果如下: 张学友
猪肉|大白菜 李明一
鸡肉 刘德华
牛肉|罗卜|青椒 陈忠和
鸭肉|花菜 李明兰
鸡肉|扁豆 ============================
不改变数据库结构,怎样实现以上的调用显示效果,如果我描述不清楚请提问。
解决方案 »
- SLQ SERVER怎么约束字段的取值的前两个字符为大写字母?
- 怎么连非默认客户端?
- 关于可以为空字段的默认值的问题
- 四舍五入
- 经常更新频繁的表如何建索引性能好
- 怎麼用script修改已經有數據的表呢,例如primary Key, Default....
- sqlserver中怎么实现表中数据的复制??
- 一个7*24小时运行的系统运行一两天就出现“可用内存小于最小查询内存,请减少min memory per query 服务器培植选项的配置值"
- 高分求教,如何实现对机关发文的文件编号的正确排序?
- SQL的意外错误捕获问题
- |zyciis| 求SQL Group By 语句 急 谢谢 第二贴 原来的查询出现重复
- 如何更新表时,数据来源于另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
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
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
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
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
CREATE TABLE 语句中的语法错误。
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 行)
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