I have a table named
posts
with columnsid, 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;
Stores data like
[{"tag": "android", "time": 122}, {"tag": "apple", "time": 140}]
intags
field.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}]');
Filtering data from table based on tag value for eg.
tag == "android"
. To achieve that I am using mysql querySELECT id, title, tags FROM posts where JSON_CONTAINS( tags , '{"tag": "android"}' ) ;
It works fine. DB Fiddle : https://www.db-fiddle.com/f/5Hw1FyL3Qv2RLtCw4tZbyh/1
Further I need to order result based on time
value in tags . tag == 'android' and order by time
.
Thanks in advance for your kind help.