如何按照记录进入的顺序为数据库条目分配轮换类别?

发布于 2024-08-31 18:30:18 字数 529 浏览 3 评论 0原文

我有一个从网站获取条目的表,当这些条目进入数据库时​​,需要将它们分配到可能随时更改的类别列表中的下一个类别。

由于这个原因,我无法做一些简单的事情,例如将第一个类别 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-09-07 18:30:18

我会做得更简单......只需制作一个包含一个条目“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.

笨笨の傻瓜 2024-09-07 18:30:18

我不确定我是否理解您的问题,但据我了解,您希望拥有

category | data
-----------------
0        | lorem
1        | ipsum
....     | ...
4        | dolor
0        | sit
...      | ...

一个独特的 auto_increment 列,并让类别为 MOD 5 本专栏的?

I'm not sure I understand your problem, but as I understand it you would like to have something like

category | data
-----------------
0        | lorem
1        | ipsum
....     | ...
4        | dolor
0        | sit
...      | ...

How about having a unique auto_increment column, and let category be the MOD 5 of this column?

窝囊感情。 2024-09-07 18:30:18

如果您需要 100% 正确的行为,听起来您需要将某些东西锁定在某处,以便所有插入正确排列。如果使用单个 SQL 语句插入数据,您也许能够避免锁定类别表。我不确定 MySQL 有何不同,但在 Oracle 中我可以这样做:

insert into my_table (id, col1, col2, category_id)
select :1, :2, :3, :4, c.id -- :1, :2, etc are bind variables. :1 corresponds to the ID.
from 
  (select 
      id, -- category id
      count(*) over (partition by 1) cnt, -- count of how many categories there are
      row_number() over (partition by 1 order by category.id) rn -- row number for current row in result set
   from category) c
where c.rn = mod(:1, cnt)

这样,在一个语句中,我根据当时存在的类别插入下一条记录。插入会自动锁定 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:

insert into my_table (id, col1, col2, category_id)
select :1, :2, :3, :4, c.id -- :1, :2, etc are bind variables. :1 corresponds to the ID.
from 
  (select 
      id, -- category id
      count(*) over (partition by 1) cnt, -- count of how many categories there are
      row_number() over (partition by 1 order by category.id) rn -- row number for current row in result set
   from category) c
where c.rn = mod(:1, cnt)

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 if count(*) requires group by in mysql; in oracle it does so I used a partition instead to count the whole result set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文