鍍金池/ 問答/數(shù)據(jù)庫/ 請問mysql 這樣的查詢需要能否實現(xiàn)?

請問mysql 這樣的查詢需要能否實現(xiàn)?

數(shù)據(jù)庫結(jié)構(gòu)
帖子積分增加的記錄表
id,tid,opint,create_time
1,1,1,1510390059
2,1,1,1510390059
3,1,3,1510390059
4,2,3,1510390059
5,2,5,1510390059
6,3,3,1510390059
7,3,2,1510390059
8,4,3,1510390059

類似這樣,

需求:
獲取create_time 為1小時內(nèi) piont增加最多的前3 的tid?

請問能否實現(xiàn)?

回答
編輯回答
舊螢火
SELECT 
    tid
FROM
    record
WHERE
    create_time >= CURRENT_TIMESTAMP - 3600000
GROUP BY tid
ORDER BY SUM(point) DESC
LIMIT 3

如果用APIJSON,可以這樣請求:

{
    "[]": {
        "Record": {
            "@column": "tid",
            "create_time{}": ">=CURRENT_TIMESTAMP-3600000",
            "@group": "tid",
            "@order": "SUM(point)-"
        },
        "count": 3
    }
}

然后服務(wù)器會返回:

{
    "[]": [
        {
            "Record": {
                "tid": 82001
            }
        },
        {
            "Record": {
                "tid": 82002
            }
        },
        {
            "Record": {
                "tid": 82003
            }
        }
    ],
    "code": 200,
    "msg": "success"
}

可以提取出tid:

{
    "Record-tid[]": {
        "Record": {
            "@column": "tid",
            "create_time{}": ">=CURRENT_TIMESTAMP-3600000",
            "@group": "tid",
            "@order": "SUM(point)-"
        },
        "count": 3
    }
}

然后服務(wù)器會返回:

{
    "Record-tid[]": [
        82001,
        82002,
        82003
    ],
    "code": 200,
    "msg": "success"
}

一個可直接在線測試的demo:
http://39.108.143.172/

{
    "Comment-userId[]": {
        "Comment": {
            "@column": "userId",
            "date{}": ">=CURRENT_TIMESTAMP-3600000",
            "@group": "userId",
            "@order": "SUM(momentId)-"
        },
        "count": 3
    }
}

用APIJSON,后端不用寫接口和文檔,前端/客戶端 定制返回JSON的內(nèi)容和結(jié)構(gòu)^_^
https://github.com/TommyLemon...

2018年2月15日 21:41