简体   繁体   English

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

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

Firstly please understand that SQL is not one of my strong areas at the moment and i am a little unsure why i cannot get the query to-do this, it seems possible. 首先请理解,SQL目前不是我的强项之一,我有点不确定为什么我无法让查询做到这一点,这似乎是可能的。

we are running a mysql server v5.1 我们正在运行mysql服务器v5.1

i have a filenotes table that i need to insert a single record into for a list of clients we have. 我有一个文件注释表,我需要在其中插入一条记录,以获取我们拥有的客户列表。

so i did this. 所以我做到了

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)

however i keep getting errors from SQL relating to the select statement, there is no indication in the error to what the problem is though, it just states. 但是我一直在从SQL中获取与select语句有关的错误,但该错误仅说明了错误,但没有指出问题所在。

"you have an error in your SQL syntax near select clienttable.clientid from clienttable where clienttable.clientid in (" “您在从clienttable中选择clienttable.clientid附近的SQL语法中有错误,其中clienttable.clientid在(“

whilst i know there is a problem there i cannot see what that problem would be. 虽然我知道有问题,但是我看不到那是什么问题。 please be aware that the field names do not match in the tables. 请注意,字段名称在表中不匹配。

this is the example i followed. 这是我遵循的示例。

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

You're mixing up two different styles of INSERT . 您正在混合两种不同样式的INSERT

To use the same method as your example, you'd need to do: 要使用与示例相同的方法,您需要执行以下操作:

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)

or use BETWEEN : 或使用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