怎么把时间最新的显示出来?

k98188 2008-08-01 05:46:08
有这样的数据库
表1 country
id cname
1 中国
2 美国
3 加拿大
表2 city
id name cid sj
1 北京 1 2008-07-31 00:00:00
2 上海 1 2008-08-01 00:00:00
3 纽约 2 2008-07-15 00:00:00
4 旧金山 2 2008-07-01 00:00:00
5 温哥华 3 2008-08-01 00:00:00

select country.cname as cname ,city.name as cs from country,city where country.id=city.cid group by country.cname having max(UNIX_TIMESTAMP(sj))
显示的结果是:
中国 北京
加拿大 温哥华
美国 纽约

我想要的结果是:让时间最新的显示
中国 上海
美国 纽约
加拿大 温哥华
...全文
66 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ten789 2008-08-02
  • 打赏
  • 举报
回复
时间用时间戳存储 一劳永逸
jacklygoodluck 2008-08-01
  • 打赏
  • 举报
回复
cname name
-------------------- --------------------
中国 上海
美国 纽约
加拿大 温哥华

(3 行受影响)

jacklygoodluck 2008-08-01
  • 打赏
  • 举报
回复
declare @t1 table(id int,cname varchar(20))
insert into @t1
select 1,'中国' union all
select 2,'美国' union all
select 3,'加拿大 '

declare @t2 table(id int,name varchar(20),cid int,sj datetime)
insert into @t2
select 1,'北京',1,'2008-07-31 00:00:00' union all
select 2,'上海',1,'2008-08-01 00:00:00' union all
select 3,'纽约',2,'2008-07-15 00:00:00 ' union all
select 4,'旧金山',2,'2008-07-01 00:00:00' union all
select 5,'温哥华',3,'2008-08-01 00:00:00'

select d.cname,b.[name] from
(select c.cid,max(c.sj) as sj from @t2 c group by cid ) a,@t2 b,@t1 d
where a.cid=b.cid and b.cid=d.id and b.sj=a.sj
jacklygoodluck 2008-08-01
  • 打赏
  • 举报
回复
declare @t1 table(id int,cname varchar(20))
insert into @t1
select 1,'中国' union all
select 2,'美国' union all
select 3,'加拿大 '

declare @t2 table(id int,name varchar(20),cid int,sj datetime)
insert into @t2
select 1,'北京',1,'2008-07-31 00:00:00' union all
select 2,'上海',1,'2008-08-01 00:00:00' union all
select 3,'纽约',2,'2008-07-15 00:00:00 ' union all
select 4,'旧金山',2,'2008-07-01 00:00:00' union all
select 5,'温哥华',3,'2008-08-01 00:00:00'


select d.cname,b.[name] from
(select c.cid,max(c.sj) as sj from @t2 c group by c.cid ) a
left join @t2 b
on a.cid=b.id
left join @t1 d
on b.id=d.id


cname name
-------------------- --------------------
中国 北京
美国 上海
加拿大 纽约

(3 行受影响)
bugx 2008-08-01
  • 打赏
  • 举报
回复
order by sj desc

时间是可以排序的

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧