生成唯一的数组值

发布于 2024-09-10 15:45:00 字数 618 浏览 1 评论 0原文

我有一个这样的问题,我无法很好地解决。我的解决方案需要太多时间,并且循环使用了灾难性的大内存。所以我需要帮助。

我有3个mysql表。

  • group_id
  • group_name

游戏

  • game_id
  • game_name

问题

  • questions_id
  • game_id
  • Question_name
  • Question_text

Question_groups

  • Question_id
  • group_id
  • order

问题如下。

问题按以下方式提出。先后。问题是我需要以这种方式在 php 中创建一个 shufle 函数,我将以这种方式将每个问题分配给每个组,例如每个组的第一个问题将是唯一的,并且不会有 2 或 3 个组得到同样的问题,第二、第三、.....第十个问题也同样。

对此的最佳解决方案是什么?

我应该使用 mysql 函数还是 php 函数?

I have a such problem which I couldn't solve in a good manner. My solution needs too much time and loop uses catastrophically big memory. So I need a help.

I have 3 mysql tables.

Gorups

  • group_id
  • group_name

games

  • game_id
  • game_name

questions

  • question_id
  • game_id
  • question_name
  • question_text

question_groups

  • question_id
  • group_id
  • order

The problem is following.

The questions are asked in following manner. One after another. The problem is that I need to make a shufle funnction in php in such way, that I will assign each question to each group in that way, that for example the first question for each group will be unique, and no 2 or 3 groups will get the same question, the same for second, third, .....tenth question.

What is the most optimal solution for this?

Should I use somehow mysql function for that or php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

青衫负雪 2024-09-17 15:45:00

解决此问题的最简单方法是为 QUESTIONS_GROUPS 中的 question_id 列定义唯一约束/索引。这会限制数据,以便该值在整个表中只能出现一次 - 如果用户尝试添加重复的 question_id 值,他们将收到唯一约束错误。

这是您需要用来在 MySQL 中定义约束的语句:

CREATE UNIQUE INDEX question_idx BTREE ON QUESTION_GROUPS(question_id)

编辑:

如果您需要支持每轮仅询问一次的问题,我假设 QUESTION_GROUPS.order 列是舍入值是多少。假设这是正确的,请将 order 列添加到唯一约束/索引,以确保唯一的值对:

CREATE UNIQUE INDEX question_idx BTREE ON QUESTION_GROUPS(question_id, order)

如果您创建了 group_idquestion_id 列作为表的复合主键,它无法满足您的要求,因为它会允许 question_id 列值重复。

我还将定义 QUESTION_GROUPS.group_id 不允许为 NULL,否则您可以将问题分配给 NULL(不存在)组,并且必须更新现有记录或删除 &使用有效的组重新创建它。

参考:

The easiest way to solve this issue is to define a unique constraint/index for the question_id column in QUESTIONS_GROUPS. This constrains the data so that the value can only occur once in the entire table - a user would receive a unique constraint error if they attempted to add a duplicate question_id value.

Here's the statement you'd need to use to define the constraint in MySQL:

CREATE UNIQUE INDEX question_idx BTREE ON QUESTION_GROUPS(question_id)

Edit:

If you need to support a question being asked only once per round, I'm assuming that the QUESTION_GROUPS.order column is what the round value is. Assuming that's correct, add the order column to the unique constraint/index to ensure unique pairs of values:

CREATE UNIQUE INDEX question_idx BTREE ON QUESTION_GROUPS(question_id, order)

If you made the group_id and question_id columns to be the composite primary key for the table, it won't work for your requirements because it would allow the question_id column value to be duplicated.

I would also define that the QUESTION_GROUPS.group_id not be allowed to be NULL, otherwise you could assign a question to a NULL (non-existent) group and would have to update the existing record or delete & recreate it with a valid group.

Reference:

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