SQL 查询列中的更多信息
SQL query for more information in column
我需要一些关于 SQL 查询的信息。这是我的 SQL table;
CustomData 是我的 table 中的列名。
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);
?>
像这样:
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'
Select CustomData
From Entities
Where CustomData LIKE '%Telefon%'
会让你走得更远
原始答案(针对 SQL Server 2016+):
CustomData
列中的数据似乎是一个有效的 JSON
数组,因此一种可能的方法是使用 OPENJSON()
和 JSON_VALUE()
(JSON
支持在 SQL Server 2016 中引入)。当您将 OPENJSON()
与默认架构一起使用时,结果是 table,其中包含列 key
、value
和 type
,因此您需要包含额外的 CROSS APPLY
运算符解析 JSON
数组中的每一项。
Table:
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"
}
]')
PHP:
<?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);
?>
更新:
SQL Server 2014 不支持 JSON
,因此一种可能的方法是使用 json_decode()
解析 PHP
部分中的 JSON
数据:
<?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 CustomData FROM Entities";
$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)){
$decoded = json_decode($row3["CustomData"], true);
foreach($decoded as $item) {
if ($item["Name"] == "Telefon") {
$json[] = $item["Value"];
}
}
}
echo json_encode($json);
sqlsrv_free_stmt($result3);
sqlsrv_close($consamba);
?>
我需要一些关于 SQL 查询的信息。这是我的 SQL table;
CustomData 是我的 table 中的列名。
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);
?>
像这样:
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'
Select CustomData
From Entities
Where CustomData LIKE '%Telefon%'
会让你走得更远
原始答案(针对 SQL Server 2016+):
CustomData
列中的数据似乎是一个有效的 JSON
数组,因此一种可能的方法是使用 OPENJSON()
和 JSON_VALUE()
(JSON
支持在 SQL Server 2016 中引入)。当您将 OPENJSON()
与默认架构一起使用时,结果是 table,其中包含列 key
、value
和 type
,因此您需要包含额外的 CROSS APPLY
运算符解析 JSON
数组中的每一项。
Table:
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"
}
]')
PHP:
<?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);
?>
更新:
SQL Server 2014 不支持 JSON
,因此一种可能的方法是使用 json_decode()
解析 PHP
部分中的 JSON
数据:
<?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 CustomData FROM Entities";
$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)){
$decoded = json_decode($row3["CustomData"], true);
foreach($decoded as $item) {
if ($item["Name"] == "Telefon") {
$json[] = $item["Value"];
}
}
}
echo json_encode($json);
sqlsrv_free_stmt($result3);
sqlsrv_close($consamba);
?>