MySQL序列号生成为ROW

时间:2016-04-10 07:35:25

标签: mysql

我需要一个QUERY来生成序列号,如:

SELECT n as seq_no FROM DUAL;

n = 5时返回结果,如:

输出:

+--------+
| seq_no |
+--------+
|  1     |
|  2     |
|  3     |
|  4     |
|  5     |
+--------+

到目前为止,我已经尝试过:

SET @n = 10;
SET @i = 1;
SET @Q = '';

WHILE @i < @n DO
    SET @Q = @Q || 'UNION ALL SELECT' || @i || ' FROM DUAL';
    set @i= @i+1;
END WHILE;

SET @Q = SUBSTRING(@Q, 10); 

PREPARE stmt1 FROM @Q; 
EXECUTE stmt1; 
DEALLOCATE PREPARE stmt1;

但它显示:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'WHILE @i&lt; @n DO SET @Q = @Q || 'UNION ALL SELECT'|| @i || “   来自第1行的DUAL''

1 个答案:

答案 0 :(得分:2)

你可以用这个

SELECT @n:=(@n := @n +1) AS seq_no 
FROM someTable
CROSS JOIN (SELECT @n:= 0) AS parameter;

使用旧的查询,你可以把它放在那里:

SELECT @n:=(@n := @n +1) AS seq_no 
FROM ( SELECT ... OLD QUERY) AS q
CROSS JOIN (SELECT @n:= 0) AS parameter;

<强>样品

MariaDB []> select * from abc;
+----+------+
| id | b    |
+----+------+
| 33 |  100 |
| 34 |  101 |
| 35 |   11 |
| 36 |   22 |
| 37 |   22 |
| 38 |   11 |
+----+------+
6 rows in set (0.00 sec)

MariaDB []> SELECT @n:=(@n := @n +1) AS seq_no  FROM ( SELECT * from abc) AS q CROSS JOIN (SELECT @n:= 0) AS parameter;
+--------+
| seq_no |
+--------+
|      1 |
|      2 |
|      3 |
|      4 |
|      5 |
|      6 |
+--------+
6 rows in set (0.00 sec)

MariaDB []>

要仅生成一个号码,您可以使用:

SELECT 0 AS seq_no UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
SELECT 4   UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
SELECT 8   UNION ALL SELECT 9

在MariaDB中,您可以完成所有

的序列引擎
MariaDB []> select seq from seq_1_to_10;
+-----+
| seq |
+-----+
|   1 |
|   2 |
|   3 |
|   4 |
|   5 |
|   6 |
|   7 |
|   8 |
|   9 |
|  10 |
+-----+
10 rows in set (0.04 sec)

MariaDB []>

下一次尝试 - 在BETWEEN你可以设置边界

SELECT *
FROM (
  SELECT (d1.seq_no*10)+d2.seq_no AS seq_no
  FROM (
    SELECT 0 AS seq_no UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
    SELECT 4   UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
    SELECT 8   UNION ALL SELECT 9
    ) AS d1
 CROSS JOIN (
    SELECT 0 AS seq_no UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
    SELECT 4   UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
    SELECT 8   UNION ALL SELECT 9 
  ) AS d2
) AS result
WHERE seq_no BETWEEN 1 AND 13
ORDER BY seq_no;

<强>样品

MariaDB []> SELECT *
    -> FROM (
    ->   SELECT (d1.seq_no*10)+d2.seq_no AS seq_no
    ->   FROM (
    ->     SELECT 0 AS seq_no UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
    ->     SELECT 4   UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
    ->     SELECT 8   UNION ALL SELECT 9
    ->     ) AS d1
    ->  CROSS JOIN (
    ->     SELECT 0 AS seq_no UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
    ->     SELECT 4   UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
    ->     SELECT 8   UNION ALL SELECT 9
    ->   ) AS d2
    -> ) AS result
    -> WHERE seq_no BETWEEN 1 AND 13
    -> ORDER BY seq_no;

+--------+
| seq_no |
+--------+
|      1 |
|      2 |
|      3 |
|      4 |
|      5 |
|      6 |
|      7 |
|      8 |
|      9 |
|     10 |
|     11 |
|     12 |
|     13 |
+--------+
13 rows in set (0.00 sec)

MariaDB []>