1

我正在尝试使用现有表(员工)emp_department_1中的 4 列( )填充我的临时表()。fname, minit, lname, bdate来自dno=1员工表的位置。有什么建议么?

mysql> select * from employee;
+----------+-------+---------+-----------+------------+--------------------------+------+--------+-----------+-----+
| Fname    | Minit | Lname   | SSN       | Bdate      | Address                  | Sex  | Salary | Superssn  | Dno |
+----------+-------+---------+-----------+------------+--------------------------+------+--------+-----------+-----+
| John     | B     | Smith   | 123456789 | 1955-01-09 | 731 Fondren, Cary, NC    | M    |  31500 | 333445555 |   5 |
| Franklin | T     | Wong    | 333445555 | 1945-12-08 | 638 Voss, Cary, NC       | M    |  42000 | 888665555 |   5 |
| Alicia   | J     | Zelaya  | 999887777 | 1958-07-19 | 3321 Castle, Apex, NC    | F    |  25000 | 987654321 |   4 |
| Jennifer | S     | Wallace | 987654321 | 1931-06-20 | 291 Berry, Garner, NC    | F    |  43000 | 888665555 |   4 |
| Rameish  | K     | Naraya  | 666884444 | 1952-09-15 | 975 Fire Oak, Angier, NC | M    |  39900 | 333445555 |   5 |
| Joyce    | A     | English | 453453453 | 1962-07-31 | 5631 Rice, Raleigh, NC   | F    |  26250 | 333445555 |   5 |
| Ahmad    | V     | Jabbar  | 987987987 | 1959-03-29 | 980 Dallas, Cary, NC     | M    |  25000 | 987654321 |   4 |
| James    | E     | Borg    | 888665555 | 1927-11-10 | 450 Stone, Cary, NC      | M    |  55000 | NULL      |   1 |
+----------+-------+---------+-----------+------------+--------------------------+------+--------+-----------+-----+

James E Borg 是唯一一个 dno=1 的人。

4

1 回答 1

0

你只需要一个简单的INSERT INTO ... SELECT...FROM陈述。建议列出 中的列(),假设列的emp_department_1顺序与我的顺序不完全匹配SELECT

INSERT INTO emp_department_1 (fname, minit, lname, bdate)
  SELECT fname, minit, lname, bdate FROM employee WHERE dno = 1

查看有关语法的MySQL语法参考INSERT...SELECT

更新

由于这是一个尚未创建的临时表,请使用以下CREATE TEMPORARY TABLE ... SELECT语法

CREATE TEMPORARY TABLE emp_department_1
   SELECT fname, minit, lname, bdate FROM employee WHERE dno = 1

不必在临时表中定义列类型,但您可以根据需要定义,也可以根据需要创建索引。

CREATE TEMPORARY TABLE emp_department_1 (
  fname VARCHAR(64) NOT NULL,
  minit VARCHAR(64) NULL,
  lname VARCHAR(64) NOT NULL,
  bdate DATE NOT NULL,
  PRIMARY KEY (fname, lname, bdate) /* not that you would actually do this */
) SELECT fname, minit, lname, bdate FROM employee WHERE dno = 1
于 2012-11-30T01:44:17.440 回答