请教insert into语句
admin 发表于 2010-04-21 | 来源:互联网 | 阅读:

我现在只知道Session["username"],但是我要插入的数据是与之对应的id(username和id在同一张表users)到另一个表examine中,请问我的insert into 语句要怎么写啊??
insert into examine (ex_u_id) values (select u_id from users where u_username=
‘" +Session["username"].ToString()+ "’)???有没有这种写法啊?

已经有11 个评论
  1. jc2008_828 说:

    insert into examine(ex_u_id) values (select u_id from users where u_username=’" +Session["username"].ToString()+ "’)可以。没发现问题。建议,为了更好的代码,建议参数化,而不是用+这样来拼接字符串

  2. 405290688 说:

    sql 语法有问题应该是insert into tableName () select () from tableName()里面是要填充的字段

  3. lchz 说:

    string sql=string.Format("insert into examine ([ex_u_id]) select u_id from users where u_username=’{0}’ ",Session["username"]);大概意思,具体细节自己修改

  4. wuwei04 说:

    阿非 真厉害。我也没注意,语法错误了。语法是:insert into destTbl(fld1, fld2) select fld1, 5 from srcTbl所以应该是:insert into examine(ex_u_id) select u_id from users where u_username=’" +Session["username"].ToString()+ "’;

  5. boysee 说:

    4楼说的这个语法我知道,问题是我插入的字段不止一个,其他的都可以用values,insert into examine ex_u_id,ex_applycontent,ex_define values (select u_id from users where u_username=’" +Session["username"].ToString()+ "’,’" + exreason + "’,'" + exdetail + "’),如果语法没错的话,为什么我添加怎么都是失败!!!

  6. TheAlien 说:

    额, 每个WHERE的限制条件, 要用 AND 连接,

  7. xydrj 说:

    sql 语法有问题

  8. mikeyao210 说:

    楼上的大侠,不要只跟我说错误嘛,有什么方法可以补救?insert into examine (ex_u_id) select u_id from users where u_username=’" + Session["username"].ToString() + "’ and (ex_applycontent,ex_define) values (‘" + exreason + "’,'" + exdetail + "’)换成这个还有语法错误吗?

  9. jc2008_828 说:

    你的语法错误了。分开,清晰点。应该是:C#代码。string username=Session["username"].ToString();string sql="insert into examine(ex_u_id,ex_applycontent,ex_define) select u_id,’{0}’,'{1}’ from users where u_username=’{3}’";sql=string.Format(sql,exreason,exdetail, username);直接写SQL语句的话,应该是:insert into examine(ex_u_id,ex_applycontent,ex_define) select u_id,’内容1′,’内容2′ from users where u_username=’名称’

  10. doing7 说:

    你不看回帖的?

  11. 96789123 说:

    感谢♂风车车.Net (用户名:xray2005),我的问题解决了!!

我要评论

评论功能因故关闭!

请加入我们的QQ群一起参与讨论:群号59400482(500人超级群)


返回首页 | 关于我们 | 联系我们 | 广告合作 | 网站地图 | 友情链接 | 版权声明