首先请理解 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/