我想动态生成sql查询。我找到了这个工具
http://querybuilder.js.org/demo.html
我有以下 JSON 对象:
{
"condition": "AND",
"rules": [
{
"id": "name",
"field": "name",
"type": "string",
"input": "text",
"operator": "equal",
"value": "zura"
},
{
"condition": "OR",
"rules": [
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": "1"
},
{
"id": "price",
"field": "price",
"type": "double",
"input": "number",
"operator": "equal",
"value": "123"
}
]
},
{
"id": "in_stock",
"field": "in_stock",
"type": "integer",
"input": "radio",
"operator": "equal",
"value": "1"
},
{
"condition": "AND",
"rules": [
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": "2"
},
{
"id": "in_stock",
"field": "in_stock",
"type": "integer",
"input": "radio",
"operator": "equal",
"value": "0"
}
]
}
]
}
现在我想生成 SQL 表以便正确保存此 JSON 数据。 有没有办法生成表格,如果是,请给我链接或请帮助我创建相同的表格
最佳答案
您的 Json 数据需要使用递归 SQL 函数进行解码。您首先需要创建一个自引用表,如下所示:
CREATE TABLE jsonCondition(
ConditionId INT IDENTITY,
ParentCondotionId INT ,
Id NVARCHAR(20),
Field NVARCHAR(20),
Type NVARCHAR(20),
Input NVARCHAR(20),
Operator NVARCHAR(20),
Value NVARCHAR(20)
)
那么请引用我的另一个 json recursive Conversion to SQL: How to generate hierarchical JSON data with Microsoft SQL Server 2016?
关于javascript - 将 Json 对象转换为 SQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43769297/