日期:2014-05-17  浏览次数:20723 次

数据库插入问题
表t_user_module
user_id   module_main   module_child   module_function   shortcut_flag
bbb           1                       1                         0                               N
ccc           1                       1                         0                               N
表t_user
user_id     rand_id
aaa             10
bbb             20
ccc             20
我现在想从表t_user中查出rand_id为20的user_id,并在表t_user_module中插入所显示的内容,请问SQL语句该怎么写?
我自己写了个,有错误,望高手指点!
insert   into   t_user_module(user_id,module_main,module_child,module_function,shortcut_flag)   value( '? ', '17 ', '0 ', '0 ', 'N ')   where   user_id=(select   user_id   from   t_user   t   where   rand_id= '20 ')

------解决方案--------------------
换种方法写
如果表t_user_module字段不多的话,需要select 的字段=t_user_module中的字段

insert into t_user_module
select user_id, '17 ', '0 ', '0 ', 'N ' from t_user t where rand_id= '20 '

另外 values 不是value
------解决方案--------------------
insert into t_user_module
select user_id, '17 ', '0 ', '0 ', 'N ' from t_user where rand_id=20
------解决方案--------------------
就是插入重复记录?

insert into t_user_module
(select user_id, '17 ', '0 ', '0 ', 'N ' from t_user t where rand_id= '20 '
union all
select user_id, '17 ', '0 ', '0 ', 'N ' from t_user t where rand_id= '20 '
)
------解决方案--------------------
insert into t_user_module
(select user_id, '17 ', '0 ', '0 ', 'N ' from t_user t where rand_id= '20 '
union all
select user_id, '1 ', '1 ', '0 ', 'N ' from t_user t where rand_id= '20 '
)

如果你插入的数据除了user_id,其他的可以手写的话就这样.
楼上的是一样的,只插1次的,需要2次不一样
还不行吗?
------解决方案--------------------
LZ說說報什么錯誤?
------解决方案--------------------
检查下哪个是主键
或者把主键删了
------解决方案--------------------
把建表sql写出来