我已經介紹了一個新的多對多表,並想知道是否有辦法做一個SELECT/INSERT與2表。mysql質量插入多對多表
表:
- 用戶
- 組
- user_groups(空)
我怎樣才能插入所有用戶到每一個組?例如用一切填充user_groups表。
user_groups包含用戶和組的forien密鑰。
更新更多的細節顯示錶結構:
--
-- Table structure for table `groups`
--
CREATE TABLE IF NOT EXISTS `groups` (
`group_id` int(6) NOT NULL AUTO_INCREMENT,
`group_name` varchar(255) NOT NULL,
PRIMARY KEY (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(6) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_address` varchar(255) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `user_groups`
--
CREATE TABLE IF NOT EXISTS `user_groups` (
`user_group_id` int(6) NOT NULL AUTO_INCREMENT,
`user_id` int(6) NOT NULL,
`group_id` int(6) NOT NULL,
PRIMARY KEY (`user_group_id`),
KEY `user_id` (`user_id`,`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
,我可以通過所有的循環爲此在PHP腳本用戶,但只是想知道這是可能的只有SQL –
字面更新一切,或一些更具體的數據? – raina77ow