我尝试在mysql中获得成功,但没有成功,但是我不知道怎么做。
我的数据是:
Cuenta
----------
A01
A02
A03
A05
A08
A10
我需要得到以下结果:
Cuenta
----------
A04
A06
A07
A09
谢谢!
答案 0 :(得分:0)
SELECT A.val
FROM (
SELECT CONCAT('A', LPAD((@var_i := @var_i + 1), 2, 0)) val
FROM (SELECT @var_i := 0) A0
CROSS JOIN (SELECT NULL UNION ALL SELECT NULL) A1 -- 2^1
CROSS JOIN (SELECT NULL UNION ALL SELECT NULL) A2 -- 2^2
CROSS JOIN (SELECT NULL UNION ALL SELECT NULL) A3 -- 2^3
CROSS JOIN (SELECT NULL UNION ALL SELECT NULL) A4 -- 2^4
CROSS JOIN (SELECT NULL UNION ALL SELECT NULL) A5 -- 2^5
CROSS JOIN (SELECT NULL UNION ALL SELECT NULL) A6 -- 2^6
CROSS JOIN (SELECT NULL UNION ALL SELECT NULL) A7 -- 2^7
LIMIT 99 -- 2^7 > 99 > 2^6, length 2 table should be cross joined at least 7 times
) A
LEFT JOIN `table` t ON A.val = t.Cuenta
WHERE t.Cuent IS NULL
;
答案 1 :(得分:0)
您可以使用所需序列中的所有值生成一个临时表,您可以使用如下准备语句:
SET @n = 10;
set @i = 1;
-- query to generate sequence
SET @t = CONCAT('SELECT 1 AS n', REPEAT(' UNION ALL SELECT @i := @i + 1', @n - 1));
-- query to insert sequence into temporary table
SET @s = CONCAT("CREATE TABLE tmp SELECT CONCAT('A', LPAD(n, 2, 0)) AS Cuenta FROM (", @t, ") t");
PREPARE stmt FROM @s;
EXECUTE stmt;
您的临时表如下所示:
+--------+
| Cuenta |
+--------+
| A01 |
| A02 |
| A03 |
| A04 |
| A05 |
| A06 |
| A07 |
| A08 |
| A09 |
| A10 |
+--------+
现在您可以连接两个表并找到所需的内容:
SELECT tmp.Cuenta
FROM tmp
LEFT JOIN tbl ON tbl.Cuenta = tmp.Cuenta
WHERE tbl.Cuenta IS NULL
;
结果将是:
+--------+
| Cuenta |
+--------+
| A04 |
| A06 |
| A07 |
| A09 |
+--------+
免责声明:这种方法适用于小型数据集,但不幸的是,对于大型数据集,行为尚不确定。