mysql - 如何获取选择子查询,其中另一个表的整个数据以json格式获取

标签 mysql mysql-workbench database-administration mysqladministrator mysql-dependent-subquery

表一

enter image description here

表2

enter image description here

我想要格式中需要的这种输出 enter image description here

+

我当前查询这个

SELECT *, (SELECT id FROM tb1 where id = 1) as data_json FROM `tb5`

我想在一列中获取表 tb1 的全部数据 像那样

id | col1 | col2 | col3 | col4 | data_json
 1 |  ab  |  ab  |  uo  | ty   | [{'col1':1, 'col2':'ok', 'col3':'yy'},{'col1':1, 'col2':'ok', 'col3':'yy'}...]

这里告诉我如何改变逻辑

如何从 tb5 获取整个 json 数据我不想使用连接和特定列提及想要整个 tb5

帮帮我......!

最佳答案

我们需要这些 json 函数来转换您的数据。

select t1.*,
    json_arrayagg(json_object('col1', t2.col1, 'col2', t2.col1, 'col3', t2.col3)) as data_json
from tb5 t1
inner join tb1 t2 on t2.id = t1.id
where t2.id = 1;

关于mysql - 如何获取选择子查询,其中另一个表的整个数据以json格式获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59281008/

相关文章:

mysql - 如何在远程服务器的asp.net应用程序中添加mysql.data.dll

sql - 为什么sql server中每个表只能创建一个聚集索引?

character-encoding - Sybase ASE : show the character encoding used by the database

postgresql - 终止和取消进程之间的区别

MySQL根据两行数据选择计数

mysql - 在 union all 中使用 order by

php - 使用 onclick 方法从父标签获取单个子值

php mysql 根据多个条件获取最新记录并统计

mysql - 我想获取 2 个日期之间的数据,但显示 1064 错误

mysql - 'SET foreign_key_checks = 1' 不再起作用