我在MySQL DB中有一个带有UNIQUE INT(10)列的表。该表非常填充,该行包含该列中非连续的整数数字条目。我想做一个查询,它会得到不在任何行中的最小数字(或n个最小数字)。
示例:该表包含列值为(1, 2, 3, 5, 7, 8, 10, 12, 15)
的行。 sql语句应返回,即五个最低的非包含值,在这种情况下为4, 6, 9, 11, 13
。
这可能与MySQL有关吗?
答案 0 :(得分:4)
您可以使用“数字”表(它对各种操作很方便):
CREATE TABLE num
( i UNSIGNED INT NOT NULL
, PRIMARY KEY (i)
) ;
INSERT INTO num (i)
VALUES
(1), (2), ..., (1000000) ;
然后:
SELECT
num.i
FROM
num
LEFT JOIN
tableX AS t
ON num.i = t.columnX
WHERE
t.columnX IS NULL
ORDER BY
num.i
LIMIT 5
或:
SELECT
num.i
FROM
num
WHERE
NOT EXISTS
( SELECT *
FROM tableX AS t
WHERE num.i = t.columnX
)
ORDER BY
num.i
LIMIT 5
另一种不使用辅助表的方法是使用MySQL变量。您可以在SQL-Fiddle, test-2中对其进行测试。输出与前一个不同(只是为了表明它可以完成):
SELECT start_id, end_id
FROM
( SELECT
IF( t.columnX <> @id, @id, NULL) AS start_id
, IF( t.columnX <> @id, t.columnX-1, NULL) AS end_id
, @rows := @rows + (t.columnX - @id) AS r
, @id := t.columnX + 1 AS running_id
FROM
tableX AS t
CROSS JOIN
( SELECT @rows := 0
, @id := 1
) AS dummy
WHERE
@rows < 5
ORDER BY
t.columnX
) AS tmp
WHERE
start_id IS NOT NULL
答案 1 :(得分:2)
这会有效,但我认为效率很低。您不需要额外的表(对于INT中的所有正数,该表将为(2^31-1)*4/1024^3 = 8GB
)。另外,我建议你看看你为什么需要这个,因为它可能不是必要的。
它还会返回范围的开始和结束,但不会返回该范围内的所有数字。 (例如,如果您有数字1和5,它将返回{0,2,4,6})
SELECT (t.num-1) AS bound FROM t
WHERE t.num-1 NOT IN (SELECT t.num FROM t)
UNION
SELECT (t.num+1) AS bound FROM t
WHERE t.num+1 NOT IN (SELECT t.num FROM t)
正如我所说,这将是非常低效的,JOIN可能会更快,但你需要基准测试。
SELECT (t.num-1) AS bound FROM t
LEFT JOIN t AS u ON t.num-1 = u.num
WHERE u.num IS NULL
UNION
SELECT (t.num+1) AS bound FROM t
LEFT JOIN t AS u ON t.num+1 = u.num
WHERE u.num IS NULL