MySQL JSON数据需要SQL查询以根据对象中的键值对对象数组进行排序

时间:2019-04-10 23:26:55

标签: mysql json mariadb mysql-json

  1. 我有一个名为posts的表,其中有列id, title, tags

     CREATE TABLE IF NOT EXISTS `posts` (
       `id` int(6) unsigned NOT NULL,
       `title` varchar(100) NOT NULL,
       `tags` json NOT NULL,
       PRIMARY KEY (`id`)
     ) DEFAULT CHARSET=utf8;
    
  2. [{"tag": "android", "time": 122}, {"tag": "apple", "time": 140}]字段中存储类似tags的数据。

      INSERT INTO `posts` (`id`, `title`, `tags`) VALUES
           ('1', 'First Post', '[{"tag": "andoroid", "time": 123}, {"tag": "mobiles", "time": 432} ]'),
           ('2', 'Second Post', '[{"tag": "apple", "time": 125}]'),
           ('3', 'Third Post', '[{"tag": "android", "time": 124}]'),
           ('4', 'Fourth Post', '[{"tag": "mobiles", "time": 472}, {"tag": "android", "time": 129}]'),
           ('5', 'Fifth Post', '[{"tag": "android", "time": 122}, {"tag": "apple", "time": 140}]'),
           ('6', 'Sixth Post', '[{"tag": "mobiles", "time": 121}, {"tag": "apple", "time": 120}]'),
           ('7', 'Seventh  Post', '[{"tag": "apple", "time": 120}, {"tag": "mobiles", "time": 130}]'),
           ('8', 'Eigth  Post', '[{"tag": "android", "time": 126}]'),
           ('9', 'Nineth  Post', '[{"tag": "mobiles", "time":132}]');
    
  3. 基于标签值过滤表中的数据,例如tag == "android"。为此,我正在使用mysql查询

    SELECT id, title, tags FROM posts where JSON_CONTAINS(
                     tags , '{"tag": "android"}'
                     ) ;
    

工作正常。 DB Fiddle:https://www.db-fiddle.com/f/5Hw1FyL3Qv2RLtCw4tZbyh/1

我还需要根据标签中time的值对结果进行排序。 tag == 'android' and order by time

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

在8之前的MySQL版本中解决此问题的唯一方法是使用存储的函数从每个标签集中找到最小的time值。像这样:

DELIMITER //
CREATE FUNCTION min_time(tags JSON)
RETURNS INT
BEGIN
  DECLARE i INT DEFAULT 0;
  DECLARE mint INT DEFAULT 9999;
  DECLARE thist INT;
  SET thist = JSON_EXTRACT(tags, CONCAT('$[', i, '].time'));
  WHILE thist IS NOT NULL DO
    IF thist < mint THEN
      SET mint = thist;
    END IF;
    SET i = i + 1;
    SET thist = JSON_EXTRACT(tags, CONCAT('$[', i, '].time'));
  END WHILE;
  RETURN mint;
END //

然后您可以使用如下查询:

SELECT id, title, tags, min_time(tags) AS min_time
FROM posts 
WHERE JSON_CONTAINS(tags , '{"tag": "android"}')
ORDER BY min_time

输出:

id  title       tags                                                                tags->'$[*].time'    min_time
4   Fourth Post [{"tag": "mobiles", "time": 472}, {"tag": "android", "time": 121}]  [472, 121]          121
5   Fifth Post  [{"tag": "android", "time": 122}, {"tag": "apple", "time": 140}]    [122, 140]          122
3   Third Post  [{"tag": "android", "time": 124}]                                   [124]               124
8   Eigth Post  [{"tag": "android", "time": 126}]                                   [126]               126

Demo on dbfiddle

这可能是过度设计的,因为它按照帖子上任何标签的最短时间排序。如果要按与android标记(正在搜索的标记)关联的时间进行排序,则可以使用以下简化查询:

SELECT id, title, tags,
       JSON_EXTRACT(tags, CONCAT(SUBSTRING_INDEX(JSON_UNQUOTE(JSON_SEARCH(tags, 'one', 'android')), '.', 1), '.time')) AS tag_time
FROM posts 
WHERE JSON_CONTAINS(tags , '{"tag": "android"}')
ORDER BY tag_time

输出:

id  title       tags                                                                tag_time
3   Third Post  [{"tag": "android", "time": 75}]                                    75
4   Fourth Post [{"tag": "mobiles", "time": 472}, {"tag": "android", "time": 121}]  121
5   Fifth Post  [{"tag": "android", "time": 122}, {"tag": "apple", "time": 140}]    122
8   Eigth Post  [{"tag": "android", "time": 126}]                                   126

Demo on dbfiddle