douxin20081125 2011-08-20 16:29 采纳率: 0%
浏览 153
已采纳

Mysql从表中选择多个id并将它们插入另一个表中

I have a mysql table called

jos_users_quizzes with the following columns:

id
quiz_i
duser_id

I have a second table called  jos_users with this columns
id
name
username
department

the user_id on first table is linked with the id of second table so quiz_id = id (jos_users) How can build a query to multiple insert the ids of a selected department into the jos_users_quizzes table... IN ONE CLICK

I am thinking a sub query or a loop will do , but no sure how to contruct the query. I need to select all user ids from selected department. For example have a list of departments, and once the department is selected , select all ids pertaining that department and insert all the Ids into the other table (quizid , (alldepartment ids)

Thanks in advance!

Code from and ASP.NET form to insert ....

  string quizidselected = DropDownList1.SelectedValue;
            string deptselected = ListBox2.SelectedValue;
            //OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes  (quiz_id,user_id) VALUES (' " + quizidselected + " ',677)");
            OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id)    SELECT id, ' " + quizidselected + " ' FROM jos_users  WHERE department = ' " + deptselected + " '"); 
  • 写回答

3条回答 默认 最新

  • dqc19941228 2011-08-20 16:40
    关注

    Based on my interpretation of what you want...

    INSERT INTO jos_users_quizzes (user_id, quiz_id)
        SELECT id, :new_quiz_id
            FROM jos_users
            WHERE department = :department
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件