建立10个下面结构的表 (tb1...tb10)
create table tb1
(
intime datetime,
tid varchar(10)
);每个表插入数据;
insert tb1 values('2011-4-1 12:00:00','1');
insert tb1 values('2011-4-2 12:00:00','1');
insert tb1 values('2011-4-3 12:00:00','1');
insert tb1 values('2011-4-4 12:00:00','1');
insert tb1 values('2011-4-5 12:00:00','1');想得出结果:
tb1.intime tb2.intime [...] tb10.intime
================================================
'2011-4-5 12:00:00' '2011-4-5 12:00:00' [...] '2011-4-5 12:00:00'
create table tb1
(
intime datetime,
tid varchar(10)
);每个表插入数据;
insert tb1 values('2011-4-1 12:00:00','1');
insert tb1 values('2011-4-2 12:00:00','1');
insert tb1 values('2011-4-3 12:00:00','1');
insert tb1 values('2011-4-4 12:00:00','1');
insert tb1 values('2011-4-5 12:00:00','1');想得出结果:
tb1.intime tb2.intime [...] tb10.intime
================================================
'2011-4-5 12:00:00' '2011-4-5 12:00:00' [...] '2011-4-5 12:00:00'
(select intime from tid1 where intime='2011-4-5 12:00:00) as t2.intime,
...
from tid1 where intime='2011-4-5 12:00:00'
有10个表啊。。
from tb1 A,tb2 B...
where a.intime=b.intime and b.intime=c.intime...
(select max(intime) from tid1) as t2.intime,
...
from tid1
为什么只输出2011-4-5 12:00:00的?其它日期的不需要,根本什么原则? (不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
简单说,就是把TB1到TB10 10个表中相同tid的最新记录数据的时间(intime),
组合起来显示出来,结果应该只有一条。其实rucypli的写法,和我现在写的差不多,只不过我加了LIMIT 1;
而且这样的写法没有判断相同的tid,这个不过是我查询记录时,临时想到的需求。
其他繁琐的写法我也想过了,就是查询这10个表,然后比较下每个表的最新intime就行了。
select tid,intime from tb1
union all
select tid,intime from tb1
...
union all
select tid,intime from tb10) a group by tid对结果再转置
下面这条SQL语句已经完全符合你提供的例子的要求了。select
(select max(intime) from tb1 where id=1) as tb1_intime,
(select max(intime) from tb2 where id=1) as tb2_intime,
(select max(intime) from tb3 where id=1) as tb3_intime,
(select max(intime) from tb4 where id=1) as tb4_intime,
(select max(intime) from tb5 where id=1) as tb5_intime,
(select max(intime) from tb6 where id=1) as tb6_intime,
(select max(intime) from tb7 where id=1) as tb7_intime,
(select max(intime) from tb8 where id=1) as tb8_intime,
(select max(intime) from tb9 where id=1) as tb9_intime,
(select max(intime) from tb10 where id=1) as tb10_intime;
你的要求:10个表中相同tid的最新记录数据的时间(intime),select tid,max(intime) from (
select tid,intime from tb1
union all
select tid,intime from tb1
...
union all
select tid,intime from tb10) a group by tid
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。