Mysql非顺序插入问题
我有一个包含 100,000 条记录的表,描述如下:
ID primary unique int (5)
Ticket unique int (5)
user varchar (20)
该表上填充的字段只有前两个,ID 和 Ticket。我现在需要根据请求将用户分配给该票证。我该怎么做?如何找到表中下一个空用户的位置?
编辑:根据要求解释场景
它是一种彩票系统。票号已经制作并填充到表中。现在,当用户注册票证时,必须将其用户名插入到用户字段中的下一个可用票证旁边。我确信有一种更简单的方法可以通过在新表上插入包含所有信息的票证来做到这一点,但这确实是一个听起来很愚蠢的要求。
那么我怎样才能找到下一个空用户在表中的位置呢?
I have a table with 100,000 records described as:
ID primary unique int (5)
Ticket unique int (5)
user varchar (20)
Only fields populated on that table are the first two, ID and Ticket. I need to now assign a user to that ticket when requested. How can i do this? How can I find where the next null user is on the table?
Edit: Explaining Scenario as requested
Its a lottery system of sorts. The Ticket numbers have already been made and populated into the table. Now when a user signs up for a ticket, their username has to be inserted next to the next available ticket, in the user field. Im sure theres a much simpler way to do this by inserting the ticket with all the information on a new table, but this is th exact requirement as dumb as it sounds.
So how can I find out where the next null user is on the table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
表的排序方案是什么?
如果 ID 号是连续的,这应该可以工作:
如果 ID 号是非连续的,并且您可以使用表的自然排序(按输入时排序)
What is the sorting scheme of the table ?
If the Id numbers are sequential this should work:
If Id numbers are NON sequential and you are OK with using the natural sort of the table (sorted as they were entered)
通过执行以下操作找到下一个 NULL 行:
当您更新时,您必须小心,不要与另一个进程也获得相同的 ID 出现竞争条件。您可以通过使用单独的表来保存
TicketAllocation
并为其指定一个指向Ticket
表的唯一外键约束来防止这种重复分配问题。Find the next NULL row by doing:
When you update though you'll have to be careful you don't have a race condition with another process also getting the same ID. You could prevent this duplicate allocation problem by having a separate table holding the
TicketAllocation
, and giving it a unique foreign key constraint pointing back to theTicket
table.您也可以在单个查询中完成此操作:
you can also do it in a single query:
这假设
ID
是自动递增的。首先找到第一条用户字段为空的记录:
然后填写:
This assumes
ID
is auto-incremented.Start by finding the first record where the user field is null:
Then fill it in: