mysql - SQL插入?将数据从一个插入到另一个

标签 mysql sql select insert

首先请理解 SQL 不是我目前的强项之一,我有点不确定为什么我无法获得查询来执行此操作,这似乎是可能的。

我们正在运行 mysql 服务器 v5.1

我有一个文件注释表,我需要将一条记录插入到我们的客户列表中。

所以我这样做了。

insert into filenote(clientid, notetype, datetime, notedetails)
    VALUES (clienttable.clientid, 'info','2011-09-29 09:00:00', 'example note')

select clienttable.clientid from clienttable 
    where clienttable.clientid in (1,2,3,4,5,6,7,8,9)

但是我不断收到与 select 语句相关的 SQL 错误,但错误中没有指示问题所在,它只是说明。

"you have an error in your SQL syntax near select clienttable.clientid from clienttable where clienttable.clientid in ("

虽然我知道那里有问题,但我看不出那个问题是什么。请注意,表中的字段名称不匹配。

这是我遵循的示例。

INSERT INTO Store_Information (store_name, Sales, Date)
SELECT store_name, Sales, Date
FROM Sales_Information
WHERE Year(Date) = 1998

最佳答案

您正在混淆两种不同风格的 INSERT

要使用与示例相同的方法,您需要执行以下操作:

INSERT INTO filenote(clientid, notetype, datetime, notedetails)
SELECT clientid, 'info','2011-09-29 09:00:00', 'example note'
FROM clienttable
WHERE clienttable.clientid in (1,2,3,4,5,6,7,8,9)

或使用BETWEEN:

INSERT INTO filenote(clientid, notetype, datetime, notedetails)
SELECT clientid, 'info','2011-09-29 09:00:00', 'example note'
FROM clienttable
WHERE clienttable.clientid BETWEEN 1 AND 9

关于mysql - SQL插入?将数据从一个插入到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7596116/

相关文章:

Javascript 与 PHP 动态通信

mysql - 如何让 NetBeans 7.4、Ruby 和 MySQL 一起运行(在 Windows 上)

MySQL 连接,即使为 0

sql - 触发检查重复项

php - 连接 MySQL 表并计算计数 - PHP 中的输出

php - 根据其他查询的值选择查询

php - 格式化 MySQL 查询的结果,就好像它是从控制台运行的一样

mysql - 请帮助使用MySQL来选择要写入的数据需要一个select语句而要创建的记录需要另一个

sql - 如何使用一个 select 语句的结果作为另一个 select 语句的列?

MYSQL:如何限制内连接?