简体   繁体   English

来自其他 SELECT 的变量 LIMIT

[英]Variable LIMIT from other SELECT

I got a table av with the columns ID, amount .我得到了一个表av ,其中列ID, amount

Also got a users table with the columns ID, timestamp还得到了一个带有列ID, timestampusers

Now each voucher relates to the first x users (ordered by timestamp), where x is the amount value of the certain voucher.现在每个凭证都与first x用户相关(按时间戳排序),其中x是特定凭证的amount值。

I did a dbfiddle with a small example我用一个小例子做了一个dbfiddle

My expected result:我的预期结果:

voucherID    userID
11           10
12           10
12           11

For better understanding:为了更好地理解:

An admin creates a voucher that is supposed to go to the first X users registered.管理员创建了一个凭证,该凭证应该发给注册的第一个 X 用户。 Each voucher has a different X每张优惠券都有不同的 X

I think this is what you want:我认为这就是你想要的:

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

I tested it in your dbfiddle and its result is:我在您的 dbfiddle 中对其进行了测试,结果是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Select 来自不同表的数据限制10,而其他表限制5 - Select data from different tables limit 10, while the other table limit 5 mySQL&#39;SELECT&#39;对变量字符串的限制 - mySQL 'SELECT' limit to variable string SELECT * FROM tbl WHERE clm LIKE CONCAT(&#39;%&#39;, <other sql query LIMIT 1> ,&#39;%&#39;) - 怎么样? - SELECT * FROM tbl WHERE clm LIKE CONCAT('%',<other sql query LIMIT 1>,'%') - HOW? 使用限制选择时错误未声明的变量 - Error undeclared variable when select using limit MySQL列类别和位置SELECT的限制为4。当类别只有3条记录时,请从其他类别中选择一个缺少位置的记录? - MySQL column category and position SELECT with limit 4. When category only has 3 records, select one from other category with the missing position? 如何从同一服务器上其他项目中的选择形式更改其他项目中的变量值? - How to change variable values that are in other project from a select form in other proyect on the same server? MySQL选择所有固定的记录和一些其他标记的记录以达到限制 - MySQL select all pinned and some other flagged record to reach limit php pdo选择多行并使用LIMIT插入到其他表 - php pdo select multiple rows and insert to other table with LIMIT 从数据库中选择并限制软件,或限制数据库(mysql) - Select from database and limit with software, or limit on database (mysql) 从两个表中进行选择,第二个表的限制为LIMIT 1 - SELECT from two tables with LIMIT 1 for second table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM