首先,创建一个名为 ints 的表,其中包含从 0 到 9 的每个数字的一条记录。
CREATE TABLE ints ( i tinyint );
然后用数据填充该表。
INSERT INTO ints VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
现在您可以使用如下查询来生成数字序列。
SELECT generator.num, COUNT(answers.id)
FROM (
SELECT a.i*10 + b.i AS num
FROM ints a, ints b
ORDER BY 1
) generator
LEFT JOIN answers ON answers.selection_number = generator.num
WHERE generator.num BETWEEN 18 AND 43
要为生成的数字添加另一个位置值,只需添加更多 ints 表的连接并相应地调整计算。以下将生成三位数字:
SELECT generator.num, COUNT(answers.id)
FROM (
SELECT a.i*100 + b.i*10 + c.i AS num
FROM ints a, ints b, ints c
ORDER BY 1
) generator
LEFT JOIN answers ON answers.selection_number = generator.num
WHERE generator.num BETWEEN 328 AND 643