简体   繁体   English

使用其他2个表中的数据插入表中

[英]INSERT into a table using data from 2 other tables

Okay so I have 3 tables; 好吧,我有3张桌子; enduserdevicemap,enduser, and device. enduserdevicemap,最终用户和设备。

I'm inserting into enduserdevicemap but I need information from both the enduser table and the device table. 我要插入到enduserdevicemap中,但是我需要来自enduser表和device表的信息。

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. 我需要获取device.pkid和enduser.pkid,但是我不断收到语法错误。 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. 首先,INSERT的属性数量应与SELECT的属性数量匹配。

Second, you don't need the last values line 其次,您不需要最后一个values

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%' 我会在enduserdevicemap(fkenduser,fkdevice,defaultprofile,tkuserassociation)中执行以下插入操作,从enduser,device中选择enduser.pkid,device.pkid,'f','1',其中enduser.userid = 1001,device.name如'% 6%'

not also that the 2 tables in the select are not joined, resulting in a cartesian. 不仅如此,select中的2个表未联接,从而形成笛卡尔坐标。

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

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