作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对编程和尝试设计一个 Web 应用程序很陌生,它提供了一个前端来更新在线类(class)数据库的数据(表格,包括用户、作业、问题、答案等)。通过类(class)管理系统提交后,所有数据都将进入该数据库。目前正在使用虚拟数据进行开发。
这个想法是允许用户从前端进行更新,而不是从后端自动进行更新(如使用触发器)。这是因为我们的数据集比较小,只需要为不熟悉mysql的用户更新表导出到数据分析程序。
使用多个连接,我创建了一个用户已经完成的任务列表,如下所示:
+----+---------------+---------------------------------------+
| id | assignment_id | quiz_name |
+----+---------------+---------------------------------------+
| 1 | 2 | Guidance Counselors (Post-Assessment) |
| 2 | 3 | Guidance Counselors (Pre-Assessment) |
| 3 | 4 | Guidance Counselors (Pre-Assessment) |
+----+---------------+---------------------------------------+
//update assignment list when button is clicked
if(isset($_POST['updatelist'])){
//query to update the assignment list
$sql_update = "DELETE FROM assignment_list;";
$sql_update .= "TRUNCATE assignment_list;";
$sql_update .= "INSERT INTO assignment_list....
#values added from joined assignment, quizzes, and user_quizzes tables
SELECT...
INNER JOIN ...
INNER JOIN ...
...
GROUP BY assignment_id";
$update_result = mysqli_multi_query($conn,$sql_update);
//success and error messages here
}
+------------------+---------+---------------+---------+-----------------------------+----------------------------+--------------------------------------+-----------+
| consolidation_id | user_id | assignment_id | quiz_id | assignment_pass_score_point | assignment_pass_score_perc | quiz_name | answer_id |
+------------------+---------+---------------+---------+-----------------------------+----------------------------+--------------------------------------+-----------+
| 1 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 175973 |
| 2 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 175981 |
| 3 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 175985 |
| 4 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 175991 |
| 5 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 175995 |
| 6 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 175999 |
| 7 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 176002 |
| 8 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 176009 |
| 9 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 176015 |
| 10 | 34 | 4 | 50 | 5.00 | 50.00 | Guidance Counselors (Pre-Assessment) | 176021 |
+------------------+---------+---------------+---------+-----------------------------+----------------------------+--------------------------------------+-----------+
//for each assignment user selects, start with an empty table and join data from corresponding course database tables
foreach($_POST['selectassign'] as $assignment){
$table = "quiz_data_update_application.consolidated_data_".$assignment;
//query updates consolidated data table(s) for selected assignments
$sql_del = "DELETE FROM $table";
$sql_trunc = "TRUNCATE $table";
//records added from joining tables in course db
$sql_ins = //sql omitted here for brevity
....
$del_sel_res = mysqli_query($conn,$sql_del);
$trunc_sel_res = mysqli_query($conn,$sql_trunc);
$ins_sel_res = mysqli_query($conn,$sql_ins);
}
最佳答案
单一查询是要走的路。不存在感知到的低效率——操作比 mysqli_query
更昂贵。 .
如果您有成千上万的表可以使用,那么我质疑拥有多个相似表的智慧。相反,有一个额外的列来指示涉及的内容。DELETE
(所有表格)和TRUNCATE
具有几乎相同的效果;没有必要两者都做。 TRUNCATE
是比较快的。
从表中删除一个索引行比截断+插入要快得多。
此外,将删除批处理到单个 SQL 语句中甚至更快。一次批量插入多行也是如此。这很容易快 10 倍。
关于php - 可以使用包含变量的表名遍历 mySQL(使用 PHP)中的表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46439744/
我是一名优秀的程序员,十分优秀!