【发布时间】:2021-08-10 19:32:36
【问题描述】:
早安,伙计们。我有一个 API 调用来为我提供在我的网络系统中注册的profilesIds。我想从我的 sqlserver 表中删除我的请求响应中不包含的内容。
我使用属性进行选择,以向我展示我与我的 slqserver 表和我的响应的共同点,然后我对剩余部分进行删除。
理论上,它应该可以工作,但没有。
请问sintax有什么错误吗?
我目前在我的 sql server 表中有 391 个寄存器,只有 389 个与我的 sqlserver 表和响应调用通信。 当我进行选择时,只给我带来共同的寄存器,idk 不起作用。
选择查询(只带我提到的 389 个寄存器,是对的)。
foreach ($result as $indUser => $userInfos){
$agentId = $userInfos['id'];
$dhInsert = date("Y-m-d H:i:s");
foreach ($userInfos['profileIds'] as $indProfile => $idProfile){
$sql = "SELECT agentId, profileId
FROM LIVEPERSON_USERPROFILES
WHERE agentId = :agentId
AND profileId = :idProfile";
$stmt = Conexao::getConnection()->prepare($sql);
$stmt->bindValue(":agentId",$agentId,PDO::PARAM_STR);
$stmt->bindValue(":idProfile",$idProfile,PDO::PARAM_STR);
$rows = $stmt->execute();
}
}
删除查询不起作用。
foreach ($result as $indUser => $userInfos){
$agentId = $userInfos['id'];
$dhInsert = date("Y-m-d H:i:s");
foreach ($userInfos['profileIds'] as $indProfile => $idProfile){
$sql = "DELETE FROM LIVEPERSON_USERPROFILES
WHERE NOT EXISTS (SELECT agentId, profileId
FROM LIVEPERSON_USERPROFILES
WHERE agentId = :agentId
AND profileId = :idProfile)";
$stmt = Conexao::getConnection()->prepare($sql);
$stmt->bindValue(":agentId",$agentId,PDO::PARAM_STR);
$stmt->bindValue(":idProfile",$idProfile,PDO::PARAM_STR);
$rows = $stmt->execute();
}
}
表格数据:
insert into testTable (agentId, profileId) values ('1536989','12345')
insert into testTable (agentId, profileId) values ('1536989','56789')
insert into testTable (agentId, profileId) values ('1536989','99999')
insert into testTable (agentId, profileId) values ('1536874','56789')
insert into testTable (agentId, profileId) values ('1536874','56984')
insert into testTable (agentId, profileId) values ('7568594','99999')
insert into testTable (agentId, profileId) values ('8895874','56984')
insert into testTable (agentId, profileId) values ('8895874','56789')
insert into testTable (agentId, profileId) values ('8895874','77777')
insert into testTable (agentId, profileId) values ('8895874','99999')
insert into testTable (agentId, profileId) values ('1235689','56789')
insert into testTable (agentId, profileId) values ('1235689','77777')
insert into testTable (agentId, profileId) values ('1245365','56984')
insert into testTable (agentId, profileId) values ('1245365','56789')
insert into testTable (agentId, profileId) values ('1245365','77777')
insert into testTable (agentId, profileId) values ('1245365','99999')
Obs:一个agentId可以有多个profileId,所以agentId是重复的。 在表格数据中,我显示agentId 1536989 有一个配置文件:12345、56789 和99999。但在请求中,这个代理只有12345、56789 个配置文件。所以我需要从表中删除技能 9999(如果发生它是成功的)。
【问题讨论】:
-
我认为应该是
NOT IN而不是NOT EXISTS -
@ADyson 喜欢这个?
$sql = "DELETE FROM LIVEPERSON_USERPROFILES WHERE NOT IN (SELECT agentId, profileId FROM LIVEPERSON_USERPROFILES WHERE agentId = :agentIdAND profileId = :idProfile)";这给我带来了致命错误:未捕获的 PDOException:SQLSTATE[42000]:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]关键字“IN”附近的语法不正确 -
抱歉,我并没有真正注意这一点,抱歉。无论如何,Barmar 的建议更好
-
请edit 提供样本数据和预期结果
标签: php sql-server pdo