Mysql非顺序插入问题

发布于 2024-11-02 06:46:45 字数 377 浏览 0 评论 0原文

我有一个包含 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 技术交流群。

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

发布评论

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

评论(4

喜你已久 2024-11-09 06:46:45

表的排序方案是什么?

如果 ID 号是连续的,这应该可以工作:

SELECT ID FROM TABLE WHERE user is null ORDER by ID LIMIT 1

如果 ID 号是非连续的,并且您可以使用表的自然排序(按输入时排序)

SELECT ID FROM TABLE WHERE user is null LIMIT 1

What is the sorting scheme of the table ?

If the Id numbers are sequential this should work:

SELECT ID FROM TABLE WHERE user is null ORDER by ID LIMIT 1

If Id numbers are NON sequential and you are OK with using the natural sort of the table (sorted as they were entered)

SELECT ID FROM TABLE WHERE user is null LIMIT 1
屌丝范 2024-11-09 06:46:45

通过执行以下操作找到下一个 NULL 行:

SELECT ID 
FROM Ticket
WHERE user IS NULL 
LIMIT 1;

当您更新时,您必须小心,不要与另一个进程也获得相同的 ID 出现竞争条件。您可以通过使用单独的表来保存 TicketAllocation 并为其指定一个指向 Ticket 表的唯一外键约束来防止这种重复分配问题。

Find the next NULL row by doing:

SELECT ID 
FROM Ticket
WHERE user IS NULL 
LIMIT 1;

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 the Ticket table.

甜是你 2024-11-09 06:46:45

您也可以在单个查询中完成此操作:

UPDATE users SET user = [username] where id = 
(select min(id) from users where user is null) 

you can also do it in a single query:

UPDATE users SET user = [username] where id = 
(select min(id) from users where user is null) 
时光沙漏 2024-11-09 06:46:45

这假设 ID 是自动递增的。

首先找到第一条用户字段为空的记录:

Select * from users where user is null order by id asc limit 1;

然后填写:

Update users set user = [username] where id = [id from select];

This assumes ID is auto-incremented.

Start by finding the first record where the user field is null:

Select * from users where user is null order by id asc limit 1;

Then fill it in:

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