我有資料庫
| 用戶 | 資訊 |
|---|---|
| 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/shujuku/507710.html
標籤:mysql sql mysql-json
上一篇:組平SQL查詢結果
下一篇:PHP致命錯誤:嘗試運行Google的示例腳本時,vendor/google/apiclient/src/Client.php:982中不存在檔案“credentials.json”
