mysql - JSON Object building in SQL. Should I build the objects in SQL or in my Application? -
i generating json objects via sql query, however, thinking should building objects in application logic (node.js) instead. given following structure , query; way query data transformed?
for full example @see http://sqlfiddle.com/#!9/81cf5/1
** expected output json **
{ id: <int> parent_comment_id: <int> content: <string> created: <int> user: { id: <int> username: <string> name_first: <string> name_last: <string> } tags: { <string>:<int|string> } }
table structure
create table `comment` ( `id` bigint(20) unsigned not null auto_increment, `parent_comment_id` bigint(20) unsigned default null, `user_id` bigint(20) unsigned not null, `content` text character set utf8mb4 collate utf8mb4_unicode_ci, `created` timestamp not null default current_timestamp, primary key (`id`) ) engine=innodb ; create table `comment_tag` ( `id` bigint(20) unsigned not null auto_increment, `comment_id` bigint(20) unsigned not null, `ref_type` varchar(64) not null, `ref_text` varchar(255) default null, `ref_id` bigint(20) unsigned default null, `created` timestamp not null default current_timestamp, primary key (`id`) ) engine=innodb ; create table `user` ( `id` bigint(20) unsigned not null auto_increment, `username` varchar(64) not null unique key, `name_first` varchar(64) not null, `name_last` varchar(64) not null, primary key (`id`) ) engine=innodb ;
current query
select `c`.`id` `id`, `c`.`parent_comment_id` `parent_comment_id`, `c`.`content` `content`, `c`.`created` `created`, concat('{', concat('"id": "', `u`.`id`, '",'), concat('"username": "', `u`.`username`, '",'), concat('"name_first": "', `u`.`name_first`, '",'), concat('"name_last": "', `u`.`name_last`, '"'), '}') `user`, if(`t`.`id` null, null, concat('{',group_concat( concat_ws( ':', concat('"',`t`.`ref_type`, '"'), `t`.`ref_id`, if(`t`.`ref_text` not null, concat('"',`t`.`ref_text`,'"'), null) )), '}')) `tags` `comment` `c` join `user` `u` on `c`.`user_id` = `u`.`id` left join `comment_tag` `t` on `c`.`id` = `t`.`comment_id` group `c`.`id`
Comments
Post a Comment