MySQL - 生成 xx 行的计数器

标签 mysql

我只想生成 xx 行,每行都有一个数字,类似这样:

SELECT 1 TO 50

结果集包含 50 行,编号从 1 到 50

谢谢

最佳答案

使用存储过程:

delimiter #

create procedure my_counter(lower_bound INT, upper_bound INT)
begin

declare v_max int unsigned default upper_bound;
declare v_counter int unsigned default lower_bound;

CREATE TABLE temp_my_counter (id int);

   while v_counter <= v_max do
    INSERT INTO temp_my_counter SELECT v_counter;   
    set v_counter=v_counter+1;
  end while;

SELECT temp_my_counter.id FROM temp_my_counter;    
DROP TABLE temp_my_counter;

end #    
delimiter ;

然后 调用 my_counter(2, 5); 将返回

2
3
4
5

关于MySQL - 生成 xx 行的计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8489024/

相关文章:

mysql - 使用复制器插件安装 wordpress 站点时出现错误 "database error write ‘Invalid default value for"

mysql - mysql 的复杂选择查询结合两列与 where 子句

mysql - 我可以在事后修改表中列的属性吗

php - 基本选择查询在页面中不返回任何内容,但在 phpMyAdmin 中获取结果

mysql - 出于性能原因,SQL JOIN 是否应该按特定顺序放置?

MySQL:无法创建表 './myhairdev/catalog_product_index_website.frm'(错误号:150)

mysql - GROUP BY 相似值并保留最新行的最短方法?

mysql - 修改 varchar 大小的问题

mysql - 将 CSV 导入 MySQL 和

php - MySql 查询以在 Chartjs 中显示数据