鉴于这两个表:
emp dept
------|--------|--------| |--------|------------|----------|
empno | name | deptno | | deptno | dname | loc |
------|--------|--------| ----------------------|----------|
7566 | Jones | 20 | | 10 | Accounting | New York |
7654 | Martin | 30 | | 20 | Research | Dallas |
7689 | Blake | 30 | | 30 | Sales | Chicago |
7782 | Clark | 10 | ----------------------------------
7839 | King | 10 |
-------------------------
其中一个问题是:
Employee 'Clark' has been transferred to the Sales department. Use
the appropriate query to reflect this change in the amp table. Make
use of a subquery to determine the department number of the Sales
department.
(刚刚发现这些表中使用的数据来自sql cookbook)
无论如何我是SQL的新手,我正在使用MySQL来完成这个问题。我考虑过使用UPDATE查询,但我无法弄清楚如何合并查询以确定部门编号。
答案 0 :(得分:5)
首先,您要为销售职位选择deptno(销售额为30):
SELECT deptno
FROM dept
WHERE dname = 'Sales'
然后你会把这个deptno用作你将要为empno = 7782命名为Clark的人设置deptno的值:
UPDATE emp SET emp.deptno =
(SELECT dept.deptno
FROM dept
WHERE dname = 'Sales')
WHERE empno = 7782;