我有資料庫
用戶 | 資訊 |
---|---|
0 | {“訊息”:[{“user_to”:1,“時間戳”:1663000000},{“user_to”:2,“時間戳”:1662000000}]} |
1 | {“訊息”:[{“user_to”:0,“時間戳”:1661000000},{“user_to”:2,“時間戳”:1660000000}]} |
2 | {“訊息”:[]} |
我想選擇在時間戳 1662000000 和 1663000000 之間發送訊息的所有用戶(任意數量的訊息,而不是全部)
我沒有外部訊息表,所以我無法從那里選擇
uj5u.com熱心網友回復:
如果您使用MySQL v8.0.x,您可以利用在子查詢JSON_TABLE
中創建JSON
格式化表。然后,在這樣的子句DISTINCT
中使用您的時間戳選擇您的用戶:WHERE
SELECT DISTINCT b.`user` FROM (
SELECT `user`, a.*
FROM `sample_table`,
JSON_TABLE(`info`,'$'
COLUMNS (
NESTED PATH '$.messages[*]'
COLUMNS (
`user_to` int(11) PATH '$.user_to',
`timestamp` int(40) PATH '$.timestamp')
)
) a
) b
WHERE b.`timestamp` BETWEEN 1662000000 AND 1663000000
ORDER BY b.`user` ASC
輸入:
用戶 | 資訊 |
---|---|
0 | {“訊息”:[{“user_to”:1,“時間戳”:1663000000},{“user_to”:2,“時間戳”:1662000000}]} |
1 | {“訊息”:[{“user_to”:0,“時間戳”:1661000000},{“user_to”:2,“時間戳”:1660000000}]} |
2 | {“訊息”:[]} |
3 | {“訊息”:[{“user_to”:0,“時間戳”:1662000000},{“user_to”:2,“時間戳”:1661000000},{“user_to”:2,“時間戳”:1660000000},{“ user_to”:2,“時間戳”:1663000000}]} |
輸出:
用戶 |
---|
0 |
3 |
db<>在這里擺弄
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/507369.html
標籤:mysql sql mysql-json
下一篇:如何從mysql中的字串陣列(即[“12”,“13”,“1”,“2”,“30”.....])中搜索字串資料(即“12”)?