WildCat

站内信数据表,如何取出某个用户最近 10 个联系人?

  •  
  •   WildCat · Oct 23, 2014 · 4295 views
    This topic created in 4226 days ago, the information mentioned may be changed or developed.

    我的站内信的数据表大约有如下字段:

    • id
    • receiver_id
    • sender_id
    • is_read
    • content
    • created_at

    eg. 如何取出 receiver_id = 1 的最近10个 sender_id ?

    Supplement 1  ·  Oct 23, 2014
    @xenme
    @yangqi
    表述有误,10个不同的sender_id
    7 replies    2014-10-24 09:52:42 +08:00
    xenme
        1
    xenme  
       Oct 23, 2014
    不是特别熟悉,大概应该这样:
    select * from table where receiver_id=1 order by creted_at desc limit 10
    yangqi
        2
    yangqi  
       Oct 23, 2014
    SELECT sender_id FROM 表 WHERE receiver_id=1 ORDER BY created_at DESC LIMIT 10;
    xenme
        3
    xenme  
       Oct 23, 2014   ❤️ 1
    可能sender有重复
    select distinct sender_id,created_at from table where receiver_id=1 order by creted_at desc limit 10
    heaton_nobu
        4
    heaton_nobu  
       Oct 23, 2014
    我只想问问为什么不用主键id,而非要用created_at,难道不是新建的id就大吗
    WildCat
        5
    WildCat  
    OP
       Oct 23, 2014
    @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
    ```

    结贴。
    staticor
        6
    staticor  
       Oct 23, 2014
    用groupby 之类的对sender_id 去个重?
    msg7086
        7
    msg7086  
       Oct 24, 2014 via iPhone   ❤️ 1
    where.distinct.first 10
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   986 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 19:45 · PVG 03:45 · LAX 12:45 · JFK 15:45
    ♥ Do have faith in what you're doing.