在MySQL过程中添加用于编号的新列

时间:2015-12-03 11:45:09

标签: mysql procedure

我想在MySQL中创建一个存储过程,它显示一个带有行号而不是行id的表的数据。像这样:

enter image description here

我尝试了这段代码:

CREATE DEFINER=`root`@`localhost` PROCEDURE `count_row`()
BEGIN
declare row_num INTEGER;
declare result INTEGER;
SELECT count(id) FROM engineers INTO row_num;
SET result=0;
WHILE(result<=row_num)
DO
SELECT result=result+1, name, date FROM engineers ;
END WHILE;
END

结果是在MySQL中创建了未定义的选项卡数量。 然后我尝试了这段代码:

CREATE DEFINER=`root`@`localhost` PROCEDURE `count_row`()
BEGIN
declare row_num INTEGER;
declare result INTEGER;
SELECT count(id) FROM engineers INTO row_num;
SET result=1;
WHILE(result<=row_num)
DO
SELECT result, name, date FROM engineers;
SET result = result +1;
END WHILE;
END

我有5个标签(等于总行数),如下所示:

enter image description here

第二个标签:

enter image description here

我想要的是只显示一个带有一个表格的标签,如问题的第一张图片所示。

1 个答案:

答案 0 :(得分:1)

尝试

SELECT @row_number:=@row_number+1 as RowNum, name as Engineer, date
FROM engineers, (SELECT @row_number:=0) as t
order by name;

SET @row_number:=0;
SELECT @row_number:=@row_number+1 as RowNum, name as Engineer, date
FROM engineers
order by name;