我需要一些有关SQL查询的信息。这是我的SQL表;
CustomData是表中的列名。
CustomData有更多列。
它表示列中的列。
如果我使用此查询;
SELECT CustomData FROM Entities
结果;
[
{
"Name": "Telefon",
"Value": "5417416039"
},
{
"Name": "Adres",
"Value": "AYDOĞAN MAH. 7.SOK. HACIHÜSEYİN APT. KAT:3 NO:6"
}
]
我想在CustomData WHERE Name = Telefon中获取价值
我该怎么做?
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE,PUT');
$serverName = "x.x.x.x"; //serverName\instanceName
$connectionInfo = array( "Database"=>"hidden","UID"=>"sa","PWD"=>"hidden", "CharacterSet" => "UTF-8");
$consamba = sqlsrv_connect( $serverName, $connectionInfo);
$sql3 = "SELECT CustomData FROM Entities WHERE EntityTypeId='1'";
$result3 = sqlsrv_query($consamba,$sql3);
$json = array();
while($row3 = sqlsrv_fetch_array($result3,SQLSRV_FETCH_ASSOC)){
$json[] = $row3;
}
echo json_encode($json);
?>
答案 0 :(得分:0)
类似这样的东西:
declare @entities table (id int, customdata nvarchar(max))
insert into @entities(id,customdata)
values (1,'
[
{
"Name": "Telefon",
"Value": "5417416039"
},
{
"Name": "Adres",
"Value": "AYDOĞAN MAH. 7.SOK. HACIHÜSEYİN APT. KAT:3 NO:6"
}
]')
select e.id, c.Value Telefon
from @entities e
cross apply openjson(e.customdata)
with(
Name nvarchar(200) '$.Name',
value nvarchar(200) '$.Value'
) c
where c.name = N'Telefon'
答案 1 :(得分:0)
Select CustomData
From Entities
Where CustomData LIKE '%Telefon%'
会让你很遥远
答案 2 :(得分:0)
原始答案(对于SQL Server 2016 +):
CustomData
列中的数据似乎是有效的JSON
数组,因此一种可能的方法是使用OPENJSON()
和JSON_VALUE()
({{1} }支持是在SQL Server 2016中引入的。当您将JSON
与默认架构一起使用时,结果是一个包含列OPENJSON()
,key
和value
的表,因此您需要包括其他type
运算符解析CROSS APPLY
数组中的每个项目。
表格:
JSON
PHP:
CREATE TABLE Entities (
EntityTypeId varchar(1),
CustomData nvarchar(max)
)
INSERT INTO Entities
(EntityTypeId, CustomData)
VALUES ('1', N'[
{
"Name": "Telefon",
"Value": "5417416039"
},
{
"Name": "Adres",
"Value": "AYDOĞAN MAH. 7.SOK. HACIHÜSEYİN APT. KAT:3 NO:6"
}
]')
更新:
SQL Server 2014不支持<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE,PUT');
$serverName = "x.x.x.x";
$connectionInfo = array("Database"=>"hidden", "UID"=>"sa", "PWD"=>"hidden", "CharacterSet" => "UTF-8");
$consamba = sqlsrv_connect($serverName, $connectionInfo);
if ($consamba === false) {
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
$sql3 = "
SELECT
e.CustomData,
JSON_VALUE(j.[value], '$.Value') AS Telefon
FROM Entities e
CROSS APPLY OPENJSON(e.CustomData) j
WHERE
EntityTypeId = '1' AND
JSON_VALUE(j.[value], '$.Name') = N'Telefon'
";
$result3 = sqlsrv_query($consamba, $sql3);
if ($result3 === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
$json = array();
while ($row3 = sqlsrv_fetch_array($result3, SQLSRV_FETCH_ASSOC)){
$json[] = $row3["Telefon"];
}
echo json_encode($json);
sqlsrv_free_stmt($result3);
sqlsrv_close($consamba);
?>
,因此一种可能的方法是使用JSON
来解析JSON
部分中的PHP
数据:
json_decode()