求个sql语句
三张表: user, group, user_group
user表列: userid, username
group表列: groupid,groupname
user_group表列: userid, groupid
现在我知道一个username,知道groupname,怎么用一个insert into 语句将她们对应的id插入到user_group表?
------解决方案--------------------insert
into user_group.userid
select uderid
from user
where username= '****(已知的username) '
union
insert
into user_group.groupid
select uderid
from user
where groupname= '****(已知的groupname) '
我也不知道对不对,插入操作中能不能用并呀?郁闷中…………
谁让我也是菜鸟……
唉……
------解决方案--------------------如下:
insert into user_group(userid, groupid) select userid, groupid from usertest, grouptest where (username= 'a ') and (groupname= 'bb ')
------解决方案--------------------insert user_group
select userid,groupid from
(select userid from [user] where @username = username)U
cross join
(select groupid from [group] where @groupname = groupname)G