Re: [SQL ] 找出不同類別的單一會員最近登入時間
指定「頻道」及「使用者」,獲取最大時間:
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://www.ptt.cc/bbs/Database/M.1404867809.A.598.html
推
07/09 10:34, , 1F
07/09 10:34, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):