繁体   English   中英

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

[英]SQL Insert? insert data from one to another

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

但是我一直在从SQL中获取与select语句有关的错误,但该错误仅说明了错误,但没有指出问题所在。

“您在从clienttable中选择clienttable.clientid附近的SQL语法中有错误,其中clienttable.clientid在(“

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

这是我遵循的示例。

INSERT INTO Store_Information (store_name, Sales, Date)
SELECT store_name, Sales, Date
FROM Sales_Information
WHERE Year(Date) = 1998
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

您正在混合两种不同样式的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
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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM