简体   繁体   English

MySQL 中的 generate_series

[英]generate_series in MySQL

What is the PostgreSQL's generate_series() equivalent in MySQL? MySQL 中 PostgreSQL 的generate_series()等价物是什么?

How to convert this query to MySQL?如何将此查询转换为 MySQL?

select substr('some-string', generate_series(1, char_length('some-string')))

Sample output from PostgreSQL:来自 PostgreSQL 的示例输出:

some-string
ome-string
me-string
e-string
-string
string
tring
ring
ing
ng
g

select generate_series(1, char_length('some-string'))

1
2
3
4
5
6
7
8
9
10
11

Final solution:最终解决方案:

CREATE TABLE `numberlist` (
 `id` tinyint(4) NOT NULL AUTO_INCREMENT,
 PRIMARY KEY (`id`)
)

INSERT INTO `numberlist` values(null)
(repeat the above query the maximum string you need)

SELECT substr('somestring', id) 
FROM numberlist 
WHERE id <= character_length('somestring')

Here is the concept, but I don't have mySQL installed on this box.这是概念,但我没有在这个盒子上安装 mySQL。 You will need to create a table of integers, using AUTO INCREMENT.您将需要使用 AUTO INCREMENT 创建一个整数表。 A table of numbers is generally a handy table to have available in a database, and would only need be created once数字表通常是数据库中可用的方便表,并且只需要创建一次

create table NumberList (id MEDIUMINT NOT NULL AUTO_INCREMENT,fill char(1))

declare @x INT
set @x=0
while @x < 20
begin
    insert into numberList values(null)
    Set @x = @x+1
end 

Then, join this table as shown below using the LIMIT clause然后,使用 LIMIT 子句加入该表,如下所示

select substr('somestring',id) 
from numberlist
limit len('somestring')

I wrote this in SQL server, but it shouldn't be too difficult to convert to mySQL...我在 SQL server 中写了这个,但是转换成 mySQL 应该不会太难......

The code below SHOULD work in mySQL下面的代码应该在 mySQL 中工作

DECLARE xx INT DEFAULT 0;
  WHILE xx < 20 DO
    insert into numberList values(null)        
    SET xx = xx + 1;
 END WHILE;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM