Okay so I have 3 tables; enduserdevicemap,enduser, and device.

I'm inserting into enduserdevicemap but I need information from both the enduser table and the device table.

insert into enduserdevicemap (fkenduser,fkdevice,defaultprofile,tkuserassociation) 
select enduser.pkid,device.pkid from enduser,device 
where enduser.userid = 1001, device.name like '%6%' 
values (enduser.pkid,device.pkid,'f','1')

I need to get device.pkid and enduser.pkid however I keep getting syntax error. I know this is wrong in so many ways...

有帮助吗?

解决方案

First the number of attributes to INSERT should be matching with the number of attributes of SELECT.

Second, you don't need the last values line

Third, are you sure you are not missing a relation between the tables: enduser and device, because this will insert all possibilities.

Example, (it might not make sense for the choice of attributes but this is just an example of how to use insert from another table):

INSERT INTO enduserdevicemap (fkenduser,fkdevice,defaultprofile,tkuserassociation) 
SELECT e.fkenduser,e.fkdevice,d.defaultprofile,d.tkuserassociation
FROM enduser e,device d 
WHERE enduser.userid = 1001 AND device.name like '%6%' 

其他提示

you can not have both select and values. it is either one or the other. I would do the following insert into enduserdevicemap (fkenduser,fkdevice,defaultprofile,tkuserassociation) select enduser.pkid,device.pkid, 'f', '1' from enduser,device where enduser.userid = 1001, device.name like '%6%'

not also that the 2 tables in the select are not joined, resulting in a cartesian.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top