About the sum calculation of mysql storage json and json data

      MySQL is a relational database. In order to record more data conveniently, the value recorded by MySQL used in work is recorded in the form of json.

      E.g:

      The player_data table structure is like this:       

DROP TABLE IF EXISTS `player_data`;
CREATE TABLE `player_data` (
  `uid` bigint(20) NOT NULL COMMENT '用户ID',
  `data` json NOT NULL COMMENT '游戏数据,以json形式包含',
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

      The player's data in player_data is recorded like this:

      key:uid --- value:{"data": { "user_prize": 0}

      I need to calculate the sum of user_prize of all players, so that

select sum(data->"$.user_prize") from player_data where data->"$.user_prize" is not null

       The storage of json in mysql is very powerful. It can store not only single-layer keys (as above), but also multi-layer keys. The application of multi-layer keys will be updated afterwards.

Guess you like

Origin blog.csdn.net/banfushen007/article/details/100898619