简体   繁体   English

使用 PHP MySQL 删除行并移动到另一个表

[英]Delete row and move to another table using PHP MySQL

I want to delete a row from my table and move that deleted row to another table.我想从我的表中删除一行并将删除的行移动到另一个表中。

Table Structure:表结构:

tbl_problem
    - problem_id
    - problem_date
    - problem_title

tbl_problem_done
    - problem_done_id
    - problem_done_date
    - problem_done_title

My query:我的查询:

$sql = "INSERT INTO tbl_problem_done SELECT * FROM tbl_problem WHERE problem_id=$problem_id";

I can copy the data using the query above.我可以使用上面的查询复制数据。 Now I want to delete the data using this query:现在我想使用此查询删除数据:

"DELETE FROM tbl_problem WHERE problem_id=$problem_id";

How do I combine this query from the previous query?如何将此查询与上一个查询结合起来?

Your approach to this is wrong, really.你的做法是错误的,真的。 This data should all be in one table.这些数据应该都在一个表中。 Then you'd UPDATE the problem to set its "done" column to true and set the date that it happened.然后您将UPDATE问题以将其“完成”列设置为 true 并设置它发生的日期。 Your current data structure is denormalised.您当前的数据结构是非规范化的。

eg table structure:例如表结构:

tbl_problem
    - problem_id
    - problem_date
    - problem_title
    - problem_done
    - problem_done_date

query:询问:

UPDATE tbl_problem SET problem_done = 1, problem_done_date = NOW() WHERE problem_id = 123

Then to select all current problems:然后到select所有当前问题:

SELECT * FROM tbl_problem WHERE problem_done = 0

And to get all done problems, obviously it's:要解决所有问题,显然是:

SELECT * FROM tbl_problem WHERE problem_done = 1

And now you also have data showing the date when it was marked done, which may be useful.现在您还有显示标记完成日期的数据,这可能很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM