【问题标题】:Ordering List of Members Closest To Their Next Award最接近下一个奖项的会员订购列表
【发布时间】:2012-04-26 17:10:46
【问题描述】:

我有两张表,一张'gift_limit_count'和一张'score'如下:

'gift_limit_count'包含:

gift_id
礼物限制
礼物金额

'score'包含:

用户 ID
用户名
user_score

当用户的“user_score”计数达到下一个最高的“gift_limit”时,他们将获得“gift_amount”。我想做的是显示一个列表,比如最接近下一个奖项的前 10 名用户。

例如:

David(user_name) 在获得 40 英镑(gift_amount)之前还剩 1 分
Suzi 在获得 20 英镑奖金之前还有 2 分的积分
在获得 40 英镑的奖励之前,伊恩还有 2 分要走
扎克在获得 30 英镑之前还有 3 分要走
...
...

【问题讨论】:

    标签: mysql


    【解决方案1】:
    Select S.user_id, S.user_name, S.user_score
        , GLC.gift_limit, GLC.gift_amount
    From    (
            Select S1.user_id
                , Min( GLC1.gift_limit ) As NextLimit
            From score As S1
                Join gift_limit_count As GLC1
                    On GLC1.gift_limit > S1.user_score
            Group By S1.user_id
            ) As Z
        Join score As S
            On S.user_id = Z.user_id
        Join gift_limit_count As GLC
            On GLC.gift_limit = Z.NextLimit
    Order By ( gift_limit - user_score )
    Limit 10
    

    【讨论】:

      【解决方案2】:

      您也许可以使用基于 min( score - gift_limit ) 的连接进行查询(使用 score

      【讨论】:

        猜你喜欢
        • 2019-08-12
        • 1970-01-01
        • 2018-08-25
        • 1970-01-01
        • 2018-07-02
        • 2014-01-13
        • 2018-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多