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 + " ',67开发者_如何学C7)");
OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id) SELECT id, ' " + quizidselected + " ' FROM jos_users WHERE department = ' " + deptselected + " '");
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
If you set the id using auto increment, then you can do something like this
insert into jos_users_quizzes (quiz_i) select id from jos_users;
It is easy if you know keyword like email address, department id or department name.
For example:
$depname = "Logistics"; // PHP // department name
$quizid = "Quiz-12"; // PHP // quiz name
Then make insert query:
<?php
$query = "INSERT INTO `to_table` (user_id, quiz_id)
SELECT id, '$quizid' FROM `from_table`
WHERE department = '$depname'";
?>
For more compatibility you can use Lower case if you obtained values from web page, like:
<?php
$query = "INSERT INTO `to_table` (user_id, quiz_id)
SELECT id, LOWER('%$quizid%')
FROM `from_table`
WHERE department like LOWER('%$depname%')";
?>
Use addslashes command for protecting database when you insert data from web page:
<?php
$query = "INSERT INTO `to_table` (user_id, quiz_id)
SELECT id, LOWER('%".addslashes($quizid)."%')
FROM `from_table`
WHERE department like LOWER('%".addslashes($depname)."%')";
?>
精彩评论