2011-09-29 121 views
0

首先请理解,SQL目前不是我的强大领域之一,我有点不确定为什么我无法获得查询 - 做到这一点,这似乎是可能的。SQL插入?插入数据从一个到另一个

我们正在运行一个MySQL服务器V5.1

我有我需要插入一条记录转化为我们的客户列表的filenotes表。

所以我做到了。

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) 

但我不断收到错误,从SQL与select语句,有错误的问题是什么,虽然没有任何迹象表明,它只是规定。

“你有你的SQL语法附近选择clienttable.clientid 从clienttable一个错误的位置clienttable.clientid在(”

虽然我知道有一个问题,在那里,我什么也看不见这个问题会。请注意,字段名不表中匹配。

这是我遵循的例子。

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

你们是不是在同一个SQL脚本来执行这两句?如果是这样,你需要在每个分支的末尾添加一个分号(或分别启动它们)。 – m0skit0

回答

2

您在混合使用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 
+0

这个例子的第一部分工作完美,因为我的真实世界的客户端ID列表不按顺序运行。 – Kristiaan

2
insert into filenote(clientid, notetype, datetime, notedetails) 
select clienttable.clientid, 'info','2011-09-29 09:00:00', 'example note' from clienttable 
where clienttable.clientid between 1 and 9 
+0

感谢您的答复,但是我不能使用between语句,因为在真正的sql示例中,clientid数字不是按顺序排列的,有近1000个id的列表,它们中没有一个遵循对方。 – Kristiaan

+0

@Kristiaan - 它们的顺序无关紧要。 ''之间'只是一个范围运算符,并且会做同样的事情。 –

+0

你是说,例如我的清单开始于60000,结束于100000,列表中没有70000个客户端ID,BETWEEN不会更新70000客户端ID记录?当然,刚好意味着从数字a到数字b以及全部数字之间? – Kristiaan

0
INSERT INTO filenote(clientid, notetype, datetime, notedetails) 
SELECT clienttable.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);