我想在MySQL中创建一个存储过程,它显示一个带有行号而不是行id的表的数据。像这样:
我尝试了这段代码:
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个标签(等于总行数),如下所示:
第二个标签:
我想要的是只显示一个带有一个表格的标签,如问题的第一张图片所示。
答案 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;