开发者

Select range of numbers? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to generate a range of numbers in Mysql

Is it possible to write a select statement that will return a list of numbers? Like

col_a  col_b
1      1
2      2
3      3
4      4
5      5

I can get one row by doing SELECT 1, 1 but how do I get more? I'm thinking something like SELECT range(1,10), range(1,10) or SELECT @x, @x FROM @x in range(1,10)... I don't really know. Not sure if this is possible.

Yes, I know this is better suited for a different language, but开发者_开发知识库 I need to do it in MySQL.


One really stupid way:

SELECT 1,1 
UNION 
SELECT 2,2
....
SELECT 10,10 


Usually this is done by creating a TEMPORARY table (often using MEMORY engine) and selecting from it.


Added: Another way might be to create a stored procedure that will create a prepared statement similar to what cularis propses in his answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜