更新MySQL表中的值

Yee*_*zus 1 mysql

鉴于这两个表:

         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   |
-------------------------
Run Code Online (Sandbox Code Playgroud)

其中一个问题是:

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.
Run Code Online (Sandbox Code Playgroud)

(刚刚发现这些表中使用的数据来自sql cookbook)

无论如何我是SQL的新手,我正在使用MySQL来完成这个问题.我考虑过使用UPDATE查询,但我无法弄清楚如何合并查询以确定部门编号.

Dre*_*den 5

首先,您要为销售职位选择deptno(销售额为30):

SELECT deptno
FROM dept
WHERE dname = 'Sales'
Run Code Online (Sandbox Code Playgroud)

然后你将把这个deptno用作你将要为empno = 7782命名为Clark的人在emp中设置deptno的值:

UPDATE emp SET emp.deptno = 
    (SELECT dept.deptno
     FROM dept
     WHERE dname = 'Sales')
WHERE empno = 7782;
Run Code Online (Sandbox Code Playgroud)