查询Tb1到TB12的表中当TID相等时 最新的 插入数据时间(intime)。
大概的表达如下,
select
a.intime AA
,b.intime BB
,c.intime CC
,d.intime DD
,e.intime EE
,f.intime FF
,G.intime GG
,h.intime HH
,i.intime II
,j.intime JJ
,k.intime KK
,l.intime LL
#,m.singindatetime road
from TB1 a,TB2 b,TB3 c,TB4 d,TB5 e,TB6 f,
TB7 g,TB8 h,TB9 i,TB10 j,
TB11 k,TB12 l
where
a.TID=b.TID=c.TID=d.TID=e.TID=f.TID=g.TID
=h.TID=i.TID=j.TID=k.TID=l.TID
order by singindatetime limit 1;
当时上面“order by singindatetime limit 1;”处的语法是错误的,
求正确写法~
大概的表达如下,
select
a.intime AA
,b.intime BB
,c.intime CC
,d.intime DD
,e.intime EE
,f.intime FF
,G.intime GG
,h.intime HH
,i.intime II
,j.intime JJ
,k.intime KK
,l.intime LL
#,m.singindatetime road
from TB1 a,TB2 b,TB3 c,TB4 d,TB5 e,TB6 f,
TB7 g,TB8 h,TB9 i,TB10 j,
TB11 k,TB12 l
where
a.TID=b.TID=c.TID=d.TID=e.TID=f.TID=g.TID
=h.TID=i.TID=j.TID=k.TID=l.TID
order by singindatetime limit 1;
当时上面“order by singindatetime limit 1;”处的语法是错误的,
求正确写法~
我那样的写法也能执行成功。关键在 order by singindatetime 的写法。
order by singindatetime 是order by intime因此,关键在 order by intime的写法我刚按ACMAIN_CHM的写法运行了次,
提示:
“SQL ERROR(1052):column 'intime' in order clause is ambiguous ”
select max(intime) from b union all
select max(intime) from c union all
select max(intime) from d
.......这样看看
加上上句,就不行了:“Unknow column 'a.TID' in 'where clause'”;
取所有表TID相等时,最近一条记录的INTIME 值,组合显示出来。
TID INTIME
============
1 2011-04-07 10:00:01
2 2011-04-07 10:00:01
....数据随机插入。
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式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)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
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'