Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL Access中的何处嵌入Select_Sql_Ms Access_Iif - Fatal编程技术网

如何在SQL Access中的何处嵌入Select

如何在SQL Access中的何处嵌入Select,sql,ms-access,iif,Sql,Ms Access,Iif,我试图将SELECT语句的结果插入到IIF中,以便更改另一列的值,但遇到了一些困难 在伪代码中,我尝试执行以下操作: FROM employees table select Employee IDs and their Teams FROM employee_locations table select Locations WHERE an employee in employee table is found in the employee_locations table, and the e

我试图将SELECT语句的结果插入到IIF中,以便更改另一列的值,但遇到了一些困难

在伪代码中,我尝试执行以下操作:

FROM employees table select Employee IDs and their Teams
FROM employee_locations table select Locations
WHERE an employee in employee table is found in the employee_locations table, and the employee's Team is blank, retrieve their Location
IIF their location is 'USA', change the Team field for that employee to 'Data Ops'
IIF their location is 'Germany', change the Team field for that employee to 'Sales'
我不明白为什么这至少对第一个IIF()不起作用:


您应该在员工和员工位置上进行连接,以匹配该方面,然后在此基础上构建更新查询

我将创建一个新的临时表来保存位置和团队之间的映射,并将其连接到查询中,以便它根据位置选择新的团队值

下面的SQL可以做到这一点:

UPDATE (employee_locations RIGHT JOIN employees ON employee_locations.employee = employees.ID) 
LEFT JOIN tbl_Temp ON employee_locations.location = tbl_Temp.location 
SET employees.Team = tbl_Temp.updateTeam
WHERE (((employees.Team) Is Null));
temp表只需要两个字段(在我的示例中为“location”和“updatetam”),其中有两条记录:(美国,数据操作)和(德国,销售)。运行查询后,可以删除临时表

您可以将此SQL复制/粘贴到查询生成器中(尽管有与您自己的表/架构相匹配的更新),然后单击“查看”(而不是“运行”)以预览在单击“运行”时要更新的记录数

更新:

要在没有临时表的情况下执行此操作,可以使用SQL中的公式进行映射,如下所示:

UPDATE employee_locations 
RIGHT JOIN employees ON employee_locations.employee = employees.ID 
SET employees.Team = IIf([location]='USA','Data Ops',IIf([location]='Germany',"Sales'",Null))
WHERE (((employees.Team) Is Null));
但是,与临时映射表相比,这将变得单调乏味(毫无疑问执行起来很慢)

更新二:

UPDATE employee_locations 
RIGHT JOIN employees ON employee_locations.employee = employees.ID 
SET employees.Team = 'Data Ops'
WHERE employees.Team Is Null AND employees.location = 'USA';

这是一个更简洁的更新查询,但您需要为要进行的每个映射在其上运行execute variances。

这可以在不创建临时表的情况下完成吗?如中所示,我是否可以将查询2环绕查询1(使用1/2的结果)?
UPDATE employee_locations 
RIGHT JOIN employees ON employee_locations.employee = employees.ID 
SET employees.Team = 'Data Ops'
WHERE employees.Team Is Null AND employees.location = 'USA';