生成唯一的数组值
我有一个这样的问题,我无法很好地解决。我的解决方案需要太多时间,并且循环使用了灾难性的大内存。所以我需要帮助。
我有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题的最简单方法是为
QUESTIONS_GROUPS
中的question_id
列定义唯一约束/索引。这会限制数据,以便该值在整个表中只能出现一次 - 如果用户尝试添加重复的question_id
值,他们将收到唯一约束错误。这是您需要用来在 MySQL 中定义约束的语句:
编辑:
如果您需要支持每轮仅询问一次的问题,我假设
QUESTION_GROUPS.order
列是舍入值是多少。假设这是正确的,请将order
列添加到唯一约束/索引,以确保唯一的值对:如果您创建了
group_id
和question_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 inQUESTIONS_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 duplicatequestion_id
value.Here's the statement you'd need to use to define the constraint in MySQL:
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 theorder
column to the unique constraint/index to ensure unique pairs of values:If you made the
group_id
andquestion_id
columns to be the composite primary key for the table, it won't work for your requirements because it would allow thequestion_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: