简体   繁体   中英

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.

we are running a mysql server 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.

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

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 .

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 :

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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