如何按照记录进入的顺序为数据库条目分配轮换类别?
我有一个从网站获取条目的表,当这些条目进入数据库时,需要将它们分配到可能随时更改的类别列表中的下一个类别。
由于这个原因,我无法做一些简单的事情,例如将第一个类别 5 映射到 ID 1、6、11、16。
我考虑过读取当前可能类别的列表,并检查最后一个类别的值插入,然后为新记录提供下一个类别,但我想如果两个请求同时进入,我可能会为它们分配相同的类别,而不是按顺序分配。
因此,我当前的想法如下:
- 锁定表(类别和记录)
- 将最新行插入记录中
- 获取最新行的 ID
- 选择 insertl 之前的行(通过使用 order by auto_inc_name desc 0, 1
- )前一行的类别,并从猫列表中获取下一个
- 更新新插入的行
- 解锁表
我不确定这是否会正常工作,并且可能有一种更简单的方法来做到这一点,所以我问:
答:这会像我在原始问题中描述的那样工作吗? B. 你有更好/更简单的方法吗?
谢谢~
I have a table which gets entries from a website, and as those entries go into the database, they need to be assigned the next category on a list of categories that may be changed at any time.
Because of this reason I can't do something simple like for mapping the first category of 5 to IDs 1, 6, 11, 16.
I've considered reading in the list of currently possibly categories, and checking the value of the last one inserted, and then giving the new record the next category, but I imagine if two requests come in at the same moment, I could potentially assign them both the same category rather then in sequence.
So, my current round of thinking is the following:
- lock the tables ( categories and records )
- insert the newest row into records
- get the newest row's ID
- select the row previous to the insertl ( by using order by auto_inc_name desc 0, 1 )
- take the previous row's category, and grab the next one from the cat list
- update the new inserted row
- unlock the table
I'm not 100% sure this will work right, and there's possibly a much easier way to do it, so I'm asking:
A. Will this work as I described in the original problem?
B. Do you have a better/easier way to do this?
Thanks ~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会做得更简单......只需制作一个包含一个条目“last_category”(unsigned tinyint not_null)的表。每次执行插入时,只需增加该值,并根据需要重置。
I would do it way simpler... just make a table with one entry, "last_category" (unsigned tinyint not_null). Every time you do an insert just increment that value, and reset as necessary.
我不确定我是否理解您的问题,但据我了解,您希望拥有
一个独特的
auto_increment
列,并让类别为MOD 5
本专栏的?I'm not sure I understand your problem, but as I understand it you would like to have something like
How about having a unique
auto_increment
column, and let category be theMOD 5
of this column?如果您需要 100% 正确的行为,听起来您需要将某些东西锁定在某处,以便所有插入正确排列。如果使用单个 SQL 语句插入数据,您也许能够避免锁定类别表。我不确定 MySQL 有何不同,但在 Oracle 中我可以这样做:
这样,在一个语句中,我根据当时存在的类别插入下一条记录。插入会自动锁定
my_table
表,直到您提交为止。它根据 ID 的模数来获取类别。 此链接向您展示如何进行一行操作- mysql 中的编号。我不确定count(*)
是否需要 mysql 中的group by
;在oracle中是这样的,所以我使用分区来计算整个结果集。If you need 100% correct behaviour it sounds like you will need to lock something somewhere so that all your inserts line up properly. You might be able to avoid locking the category table if you use a single SQL statement to insert your data. I'm not sure how MySQL differs but in Oracle I can do this:
This way in one statement I insert the next record based on the categories that existed at that moment. The insert automatically locks the
my_table
table until you commit. It grabs the category based on the modulus of the ID. This link shows you how to do a row-number in mysql. I'm not sure ifcount(*)
requiresgroup by
in mysql; in oracle it does so I used a partition instead to count the whole result set.