我正在asp.net上创建一个Web表单,允许最终用户将基于选定部门的多个用户分配到测验中。
数据库是mysql数据库,因为我使用joomla
the tables on mysql are:
jos_users_quizzes with the following columns:
id
quiz_id
user_id
I have a second is called called jos_users with this columns
id
name
username
department
第一个表上的user_id将是第二个表jos_users的id的euqal
所以quiz_id = id(jos_users)
如何构建查询以将多个所选部门的ID插入到jos_users_quizzes表中...单击一下,我有一个测验ID的下拉列表和一个用户ID,部门
我需要从所选部门中选择所有用户ID,并将这些ID插入user_quizzes表。
代码来自和ASP.NET表单插入....
private void InsertRecords(StringCollection sc)
{
string ConnectionString = @"driver={MySQL ODBC 5.1 Driver};server=localhost;database=db_dhruniversity;uid=root;pwd=;";
OdbcConnection conn = new OdbcConnection(ConnectionString);
try
{
conn.Open();
string quizidselected = DropDownList1.SelectedValue;
string deptselected = ListBox2.SelectedValue;
// OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id) SELECT uid, ' " + quizidselected + " ' FROM dhruprofile WHERE department = ' " + deptselected + " '");
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
答案 0 :(得分:1)
尝试运行:
Insert into jos_users_quizzes(quiz_id, user_id) select (your_quiz_id, user_id) from jos_users where department='selectd_depatment'