如何在 mysql 5.7 中統計 json 數組中特定值的計數
在 mysql 5.7 中無法使用 json_table 函數統計 json 數組中特定值的計數。一種替代方法是使用 json_search 函數,如下所示:
SELECT count( JSON_SEARCH( tags, 'one', "3467562849402896" ) ), count( JSON_SEARCH( tags, 'one', "3467562861985809" ) ), count( JSON_SEARCH( tags, 'one', "3465044211793921" ) ) FROM XXXXXXXXXX
登錄后復制
在這個查詢中:
- xxxxxxxxxx 是包含 json 列 tags 的表名。
- json_search 函數用于在 tags 數組中查找特定值(在本例中為 “3467562849402896”、”3467562861985809″ 和 “3465044211793921”)。
- count 函數計算每個值的出現次數。