如何插入 table WHERE 从选择中获取的值之一
How to INSERT INTO table WHERE one of the values taken from selection
我有三个表:
— 用户组
— user 其中包含 usergroupid 外键
— subscribeforum with subscribeforumid (auto incr), userid, forumid, emailupdate
我需要将属于用户组 2 和 12 的所有用户插入 subscribeforum(其余值:forumid 2 和 emailupdate 2)
下面的当然不行,因为它"returns more than one record"
INSERT INTO subscribeforum ()
VALUES ((SELECT userid
FROM user
WHERE (usergroupid = 2) OR (usergroupid = 12)), 2, 2)
我曾按以下逻辑思考过:
INSERT INTO subscribeforum (userid, forumid, emailupdate)
VALUES (uid, 2, 2)
WHERE uid IN
(SELECT userid FROM user WHERE (usergroupid = 2) OR (usergroupid = 12))
但是,我不知道正确的完成方法。
有人知道怎么做吗?
试试这个:
INSERT INTO subscribeforum (userid, forumid, emailupdate)
SELECT userid ,2, 2 FROM user WHERE (usergroupid = 2) OR (usergroupid = 12)
GROUP BY userid
我有三个表:
— 用户组 — user 其中包含 usergroupid 外键 — subscribeforum with subscribeforumid (auto incr), userid, forumid, emailupdate
我需要将属于用户组 2 和 12 的所有用户插入 subscribeforum(其余值:forumid 2 和 emailupdate 2)
下面的当然不行,因为它"returns more than one record"
INSERT INTO subscribeforum ()
VALUES ((SELECT userid
FROM user
WHERE (usergroupid = 2) OR (usergroupid = 12)), 2, 2)
我曾按以下逻辑思考过:
INSERT INTO subscribeforum (userid, forumid, emailupdate)
VALUES (uid, 2, 2)
WHERE uid IN
(SELECT userid FROM user WHERE (usergroupid = 2) OR (usergroupid = 12))
但是,我不知道正确的完成方法。
有人知道怎么做吗?
试试这个:
INSERT INTO subscribeforum (userid, forumid, emailupdate)
SELECT userid ,2, 2 FROM user WHERE (usergroupid = 2) OR (usergroupid = 12)
GROUP BY userid