作者cutekid (可爱小孩子)
看板Database
标题Re: [SQL ] 找出不同类别的单一会员最近登入时间
时间Wed Jul 9 09:03:26 2014
指定「频道」及「使用者」,获取最大时间:
Ex. 频道 = 100, 使用者 = 'abcd'
select max(time) as maxTime
from member
where aid = 100 and user = 'abcd'
依「频道」及「使用者」group,获取最大时间:
select aid,user,max(time) as maxTime
from member
group by aid,user
※ 引述《terrybob (罪云樵)》之铭言:
: (针对 SQL 语言的问题,用这个标题。请用 Ctrl+Y 砍掉这行)
: 资料库名称:
: mysql
: 资料库版本:
: 内容/问题描述:
: 资料表-member
: ------------------------------------
: id aid type user time
: ====================================
: 1000 100 a abcd 9999 <==
: 999 100 b efgh 8888
: 998 99 a aaaa 7777
: 997 99 b abcd 6666 <==
: 996 98 a cccc 5555
: 995 100 a abcd 4444
: 994 100 a abcd 3333
: 993 100 b abcd 2222
: ....................................
: 1 1 a aaaa 1
: ------------------------------------
: id是自动编号,aid是频道代号,type是种类(只有a、b)、
: user是使用者帐号,time是每次登入时间。
: 我现在要抓出,在同一个aid+user之下,离现在最近的登入时间
: 假设条件:aid=100、type=a、b、user=abcd、以time新到旧,
: 希望查出如下表
: ------------------------------------
: id aid type user time
: ====================================
: 1000 100 a abcd 9999
: 997 99 b abcd 6666
: ------------------------------------
: 现在sql是:
: select * from `member`
: where `aid`=100 and user="abcd" and `type` in ("a", "b")
: group by `type` order by `time` desc
: 结果在group by `type`後,时间不会从最新到最旧…
: 不group by的话,就会列出一堆资料…卡关了!
: 请高手们相救,感谢。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.221.80.36
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Database/M.1404867809.A.598.html
1F:推 terrybob:谢谢大家的帮助。 07/09 10:34