Mysql从一个表中选择多个id并将它们插入到另一个表中
我有一个名为 user_id 的 mysql 表
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
,第一个表与第二个表的 id 链接,所以 quiz_id = id (jos_users) 如何构建一个查询以将选定部门的 id 多次插入 jos_users_quizzes 表中...一键单击
我认为子查询或循环就可以了,但不确定如何构建查询。 我需要从所选部门中选择所有用户 ID。例如,有一个部门列表,选择该部门后,选择与该部门相关的所有 ID,并将所有 ID 插入到另一个表中(quizid,(alldepartment ids)
提前致谢!
代码来自 ASP.NET 表单以插入....
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 + " '");
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 + " '");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我对你想要的的解释......
Based on my interpretation of what you want...
如果您使用自动增量设置 id,那么您可以执行以下操作
If you set the id using auto increment, then you can do something like this
如果您知道电子邮件地址、部门 ID 或部门名称等关键字,这很容易。
例如:
然后进行插入查询:
为了获得更高的兼容性,如果您从网页获取值,可以使用小写,例如:
从网页插入数据时使用addslashes命令来保护数据库:
It is easy if you know keyword like email address, department id or department name.
For example:
Then make insert query:
For more compatibility you can use Lower case if you obtained values from web page, like:
Use addslashes command for protecting database when you insert data from web page: