0

我有一张必须确认他们对事件的帮助的人的表格。本次活动容量有限。我想计算确认人数并在同一个原子查询中更新确认状态。

假设 PEOPLE 表有 id 和确认字段。

我尝试过这样的事情:

UPDATE PEOPLE
SET confirmed = 'Y'
WHERE
    id = 'ID_VALUE'
AND
    (
        SELECT amount
        FROM
        (
            SELECT
                COUNT(*) AS amount
            FROM
                PEOPLE AS p
            WHERE
                p.confirmed = 'Y'
        ) AS aux1
    ) < CAPACITY_LIMIT

表格示例:

-------------------
| id  | confirmed |
-------------------
| 1   | N         |
-------------------
| 2   | N         |
-------------------
| ... | ...       |
-------------------
| 10  | N         |
-------------------

如果 CAPACITY_LIMIT = 5 并且我尝试从 id = 1 到 id = 10 依次确认,我想执行更新(确认 = 'Y')直到 id = 5。

我确定我错过了一些东西,但我不知道它是什么。

提前致谢。问候。

4

3 回答 3

1

试试这个:

UPDATE
    PEOPLE AS p1
INNER JOIN
    (SELECT COUNT(*) AS numConfirmed FROM PEOPLE AS p2 WHERE p2.confirmed = 'Y') aux
SET
    p1.confirmed = 'Y'
WHERE
    p1.id = 'ID_VALUE'
    AND aux.numConfirmed < CAPACITY_LIMIT;
于 2013-01-30T12:40:44.350 回答
0

试试这个

SET @ct := (SELECT
                COUNT(*) AS amount
            FROM
                PEOPLE AS p
            WHERE
                p.confirmed = 'Y');
UPDATE PEOPLE
SET confirmed = 'Y'
WHERE
    id = '1'
AND CAPACITY_LIMIT < @ct
     ;
于 2013-01-30T12:43:42.870 回答
0

falvarez,您可能想要验证您的输入('ID_VALUE',CAPACITY_LIMIT)是否正确,包括数据类型和实际值,因为相同的查询对我来说没有问题。以下是我对测试表运行的查询(您可以看到查询框架与您的完全相同):

update t_order set customer ='NewCustomerName' where id = 124 and (select amount from (select count(*) as amount from t_order where customer is not null) as total) < 10

于 2013-01-30T12:52:19.353 回答