sql - 如何在 SQL 中查询高度抽象和简化的节点/链接图,以返回嵌套的 JSON 对象?
问题描述
所以每次我从一个具体的数据模型开始(假设一个有 50 个不同的表,每个表有 10-20 个属性),我总是不断地对它进行雕琢和改进,直到最终我回到节点和链接处,或者干脆只是节点链接(一种东西)。然后我使用节点链接并为每个本机/原始数据库数据类型(日期时间/字符串/整数/等)创建 1 个表,并对所有内容进行索引。这把我带到了这里......
假设我有一个通用数据模型。
CREATE TABLE object_types (
id,
parent_id,
name,
);
CREATE TABLE string_types (
id,
parent_object_id,
value,
name,
);
CREATE TABLE property_types (
id,
parent_object_id,
value_id,
name,
);
现在我想执行一个查询,最终产生以下 JSON 结构:
{
id: 100,
name: 'foo',
links: [
{ id: 200, url: 'https://hello.world' },
{ id: 201, url: 'https://hello2.world' }
]
}
真正看起来更像是:
{
id: 100
name: 'foo',
}
{ id: 200, url: 'https://hello.world', user_id: 100 },
{ id: 201, url: 'https://hello2.world', user_id: 100 }
对象大致存储为:
# object_types
id,parent_id,name
1,0,"instanceof" # user type, instance of type 0
2,0,"instanceof" # link type, instance of type 0
100,1,"instanceof" # user instance, type user
200,2,"instanceof" # link instance, type link
# string_property_types
id,parent_object_id,value,name
1,1,"user","type" # property "type" (name) on user type
2,2,"link","type" # property "type" (name) on link type
500,100,"foo","name" # user.name
2000,200,'https://hello.world',"url"
2001,201,'https://hello2.world',"url"
# property_types
id,parent_object_id,value_id,name
1000,200,100,"user_id" # property user_id on link 1
1001,201,100,"user_id" # property user_id on link 2
你怎么能一口气执行这样的查询?我可以减少查询,首先查询图中的第一个节点,然后是下一层,然后是下一层。但是,这可以在您一次查询整个结构的情况下完成吗?
我想它不是一个图,而是一个树,所以也许可以一口气查询!
解决方案
推荐阅读
- scala - 使用 fs2 将流值列表转换为值流
- spring-boot - Spring + SpringSecurity - 未调用AuthenticationEntryPoint
- java - 使用 Java EE 依赖项在 Spring 中创建名为“entityManagerFactory”的 bean 时出错
- javascript - 引用一个全局变量,而封装的变量可用
- c++ - 在 natvis 中使用 std::type_info 进行强制转换
- kotlin-native - 如何为 Kotlin/Native 启用“宽松”内存模型模式?
- java - 以编程方式将地图片段添加到容器将其添加到底部
- c# - 我正在尝试使用统一制作游戏,但我的 Jump 功能不起作用
- python - Python-ValueError:环境变量超过 32767 个字符
- javascript - 将 json 一行解析为 html