简体   繁体   English

mysql-更新,插入或删除行值列表

[英]mysql - update, insert, or delete a list of row values

My situation is for tracking order ids. 我的情况是跟踪订单ID。 I have a separate table which is linked to a main account through account_id . 我有一个单独的表,该表通过account_id链接到主帐户。

In my account management I allow the order id's to be added, edited, deleted. 在我的帐户管理中,我允许添加,编辑或删除订单ID。 For example, in my form I would show the order ID's as order1,order2,order3,order4. 例如,在我的表单中,我将订单ID显示为order1,order2,order3,order4。 While this shows them in a comma sep string each has its own row in the accounts_orders table. 尽管这用逗号分隔的字符串显示了它们,但是accounts_orders表中的每个行都有自己的一行。

What logic should be used here as far as editing and updating the database? 就编辑和更新数据库而言,此处应使用什么逻辑? All I can think of is a comparison between my new list and the current list in the database. 我所能想到的只是数据库中新列表与当前列表之间的比较。

If in new list, but not in current... add it. 如果在新列表中,但不在当前列表中...添加它。 If not in new list, but in current... delete it. 如果不在新列表中,但在当前列表中...将其删除。

Is there any easier way to go about this? 有没有更简单的方法可以做到这一点? I do not want to delete all records and insert new ones... as that would increase my primary key much more than necessary. 我不想删除所有记录并插入新记录...,因为那样会使我的主键增加得过多。

I am not looking for a code solution, but more of the logic as it seems there must be an easier way to update/insert/delete the new list of values with that which is in the database so they are equal. 我不是在寻找代码解决方案,而是更多的逻辑,因为似乎必须有一种更简单的方法来用数据库中的值更新/插入/删除新的值列表,以便它们相等。

accounts_orders
---------------  
order_id (primary auto inc)
account_id (foreign key)
order_ref (unique)

Consider the following... 考虑以下...

CREATE TABLE account_orders
(account_id INT NOT NULL,order_id INT NOT NULL,PRIMARY KEY(account_id,order_id));

INSERT INTO account_orders VALUES
(1,101),(1,102),(1,103);

Now, you can add, edit or delete/dis-associate accounts and orders 现在,您可以添加,编辑或删除/取消关联帐户和订单

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

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