以 COUNT 为条件的 INSERT

发布于 2024-10-10 00:37:45 字数 223 浏览 0 评论 0原文

如何构造一个 MySQL INSERT 查询,该查询在表中已满足某些条件的行数小于 20 时执行,否则失败?

也就是说,如果表有 18 行满足条件,则 INSERT 应该继续。如果表中有 23 行满足条件,则 INSERT 应该失败。

为了原子性,我需要在单个查询中表达这一点,因此两个请求不能同时 INSERT,每个请求都“相信”只有 19 行满足条件。

谢谢。

How can I construct a MySQL INSERT query that only executes if the number of rows satisfying some condition already in the table is less than 20, and fails otherwise?

That is, if the table has 18 rows satisfying the condition, then the INSERT should proceed. If the table has 23 rows satisfying the condition, then the INSERT should fail.

For atomicity, I need to express this in a single query, so two requests can not INSERT at the same time, each in the 'belief' that only 19 rows satisfy the condition.

Thank you.

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

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

发布评论

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

评论(4

最笨的告白 2024-10-17 00:37:45

怎么样:

INSERT INTO TargetTable(Column1, Column2, ...)
    SELECT 'Value For Column 1', 'Value For Column 2', ...
      FROM Dual
     WHERE (SELECT COUNT(*) FROM TargetTable WHERE ...Some Condition...) < 20;

如果不满足WHERE子句,则不插入行;如果满足 where 子句,则插入一行。

您可以采用相同的机制从另一个表中进行选择(而不是从 DUAL 中选择单行值)。

What about:

INSERT INTO TargetTable(Column1, Column2, ...)
    SELECT 'Value For Column 1', 'Value For Column 2', ...
      FROM Dual
     WHERE (SELECT COUNT(*) FROM TargetTable WHERE ...Some Condition...) < 20;

If the WHERE clause is not satisfied, no row is inserted; if the where clause is satisfied, then one row is inserted.

You can adapt the same mechanism to select from another table (instead of a single row of values from DUAL).

我最亲爱的 2024-10-17 00:37:45

我唯一能做的就是锁定表、计算记录、执行操作、解锁表。

LOCK TABLES t WRITE;

perform count

decide - to insert or not in your programming language

UNLOCK TABLES;

The only thing I can get of is just lock the table, count records, perform operations, unlock the table.

LOCK TABLES t WRITE;

perform count

decide - to insert or not in your programming language

UNLOCK TABLES;
少钕鈤記 2024-10-17 00:37:45
declare @varaibleOne int;

WAITFOR DELAY '00:00:02';
select
@varaibleOne=SUM(case when (condition satisfied) then 1 else 0 end)
from table where (if you have anything)

IF(@varaibleOne<=20)
begin
insert into table ()
end

等待声明不确定..你可以谷歌它..

declare @varaibleOne int;

WAITFOR DELAY '00:00:02';
select
@varaibleOne=SUM(case when (condition satisfied) then 1 else 0 end)
from table where (if you have anything)

IF(@varaibleOne<=20)
begin
insert into table ()
end

The wait for statement is not sure.. u can google it..

你的背包 2024-10-17 00:37:45

我知道让查询实际抛出错误的唯一方法是使用SIGNAL 命令 - 只能在 MySQL 5.5 中的存储过程中访问。

创建一个存储过程来体现此任务是否可以接受?如果是这样,就有可能做你想做的事。

否则,您唯一的选择是运行两个查询 - 一个用于检查行数,另一个用于在有足够的行满足您的条件时插入新行。

这也可以在存储过程(或函数)中完成,以便您可以通过代码中的一个查询来调用它。

The only way I know of to get a query to actually throw an error is by using the SIGNAL command - which is only accessible in MySQL 5.5, inside of stored procedures.

Is it acceptable to create a stored procedure to embody this task? If so, it is possible to do what you want.

Otherwise, about your only option is to run two queries - one to check the number of rows, and another to insert new rows if there are enough rows to meet your condition.

This could also be done in a stored procedure (or a function), so that you could call it with one query from your code.

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