来自其他 SELECT 的变量 LIMIT

Variable LIMIT from other SELECT

我得到了一个 table avID, amount.

还有 users table 列 ID, timestamp

现在每张优惠券与 first x 个用户相关(按时间戳排序),其中 x 是特定优惠券的 amount 值。

我用一个小例子做了一个dbfiddle

我的预期结果:

voucherID    userID
11           10
12           10
12           11

为了更好地理解:

管理员创建了一张凭证,该凭证应该发给前 X 位注册用户。每张优惠券都有不同的X

我想这就是你想要的:

with
u as (
  select userID, timestampMock, 
    row_number() over (order by timestampMock) as rn
  from users
)
select
  av.voucherID, u.userID
from av
join u on u.rn <= av.amount
order by av.voucherID, u.userID

我在你的 dbfiddle 中测试了它,结果是:

voucherID   userID
---------   ------
11          10
12          10
12          11