将新行插入连接到数据库上另一个 table 的 table
Insert new rows into table that's connected to another table on Database
我在 Database
中有两个 table:菜单和公司。两者由 CompanyID
连接。在company
table中有将近100行不同公司的信息。
我可以通过这个 sql
query
:
在菜单 table 中插入一行
INSERT INTO `menu` (`CompanyID`, `MenuName`, `MenuLink`, `ImageLink`,
`ListOrder`, `Popup`, `ParentMenuID`, `IsActive`, `IsAdmin`) VALUES
('11', 'Holidays', 'lookups/Holidays.aspx', 'images/Archive.png', '11',
'1', '1807', '1', '0');
我这里把CompanyID
设置为11。但我不想为每个 CompanyID 插入 query
,所以是否有办法为所有公司插入?
INSERT INTO menu (CompanyID
, MenuName
, MenuLink
, ImageLink
, ListOrder
, Popup
, ParentMenuID
, IsActive
, IsAdmin)
SELECT CompanyID
, 'Holidays'
, 'lookups/Holidays.aspx'
, 'images/Archive.png'
, '11'
, '1'
, '1807'
, '1'
, '0'
FROM companies
Try This one
INSERT INTO menu (CompanyID, MenuName, MenuLink, ImageLink, ListOrder, Popup, ParentMenuID, IsActive, IsAdmin)
SELECT CompanyID, 'Holidays', 'lookups/Holidays.aspx', 'images/Archive.png'
, '11', '1', '1807', '1', '0' FROM companies
Note : Replace these (CompanyID, 'Holidays', 'lookups/Holidays.aspx', 'images/Archive.png', '11', '1', '1807', '1', '0') fields with correspondence fields in side companies table
我在 Database
中有两个 table:菜单和公司。两者由 CompanyID
连接。在company
table中有将近100行不同公司的信息。
我可以通过这个 sql
query
:
INSERT INTO `menu` (`CompanyID`, `MenuName`, `MenuLink`, `ImageLink`,
`ListOrder`, `Popup`, `ParentMenuID`, `IsActive`, `IsAdmin`) VALUES
('11', 'Holidays', 'lookups/Holidays.aspx', 'images/Archive.png', '11',
'1', '1807', '1', '0');
我这里把CompanyID
设置为11。但我不想为每个 CompanyID 插入 query
,所以是否有办法为所有公司插入?
INSERT INTO menu (CompanyID
, MenuName
, MenuLink
, ImageLink
, ListOrder
, Popup
, ParentMenuID
, IsActive
, IsAdmin)
SELECT CompanyID
, 'Holidays'
, 'lookups/Holidays.aspx'
, 'images/Archive.png'
, '11'
, '1'
, '1807'
, '1'
, '0'
FROM companies
Try This one
INSERT INTO menu (CompanyID, MenuName, MenuLink, ImageLink, ListOrder, Popup, ParentMenuID, IsActive, IsAdmin)
SELECT CompanyID, 'Holidays', 'lookups/Holidays.aspx', 'images/Archive.png'
, '11', '1', '1807', '1', '0' FROM companies
Note : Replace these (CompanyID, 'Holidays', 'lookups/Holidays.aspx', 'images/Archive.png', '11', '1', '1807', '1', '0') fields with correspondence fields in side companies table