如何创建新的MySQL表作为主表中字段的子集?

时间:2012-11-18 16:54:00

标签: mysql

我有一个包含大约30个字段的MySQL表。我需要创建一个包含这些字段子集的第二个表,再加上CURDATE()来跟踪其支出的历史记录。我遇到语法错误,但也许我的方法开始时是不正确的。请回顾一下,谢谢!

INSERT INTO history (customer_id, name, amount, date_recorded)
set date_recorded = CURDATE()
SELECT customer_id, name, amount
FROM master;

我在上面的示例中尝试在MySQL中执行的操作是从主客户列表中复制customer_id,name,amount的字段,并包括今天的日期。历史记录表的记录布局如下:

+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| customer_id   | int(11)          | NO   | PRI | NULL    |                |
| name          | varchar(255)     | NO   |     | NULL    |                |
| amount        | int(11)          | NO   |     | NULL    |                |
| date_recorded | date             | NO   |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

1 个答案:

答案 0 :(得分:1)

试试这个::

INSERT INTO history (customer_id, name, amount, date_recorded)

SELECT customer_id, name, amount, CURDATE()
FROM master

另一种方法是::

INSERT INTO history (customer_id, name, amount, date_recorded)

SELECT customer_id, name, amount, DATE(now())
FROM master