我的站内信的数据表大约有如下字段:
eg. 如何取出 receiver_id = 1 的最近10个 sender_id ?
1
xenme 2014-10-23 21:50:57 +08:00
不是特别熟悉,大概应该这样:
select * from table where receiver_id=1 order by creted_at desc limit 10 |
2
yangqi 2014-10-23 21:51:20 +08:00
SELECT sender_id FROM 表 WHERE receiver_id=1 ORDER BY created_at DESC LIMIT 10;
|
3
xenme 2014-10-23 21:52:32 +08:00 1
可能sender有重复
select distinct sender_id,created_at from table where receiver_id=1 order by creted_at desc limit 10 |
4
heaton_nobu 2014-10-23 21:59:45 +08:00
我只想问问为什么不用主键id,而非要用created_at,难道不是新建的id就大吗
|
5
WildCat OP @xenme 感谢。已经找到:
http://guides.rubyonrails.org/active_record_querying.html ```ruby query = Client.select(:name).distinct # => Returns unique names query.distinct(false) # => Returns all names, even if there are duplicates ``` 结贴。 |
6
staticor 2014-10-23 23:10:45 +08:00
用groupby 之类的对sender_id 去个重?
|
7
msg7086 2014-10-24 09:52:42 +08:00 via iPhone 1
where.distinct.first 10
|