MySQL查询结果添加值固定列和自增列

测试数据准备:

create table student(
stu_id int primary key,
stu_name varchar(20),
chinese_score int,
math_score int);

insert into student values(1,'Tom',90,80);
insert into student values(2,'Mike',70,90);
insert into student values(3,'Jane',80,60);

在这里插入图片描述

MySQL添加值固定列

select 固定值 as 列名 from ...

示例:
select * ,'男' as sex from student;//此时sex变为字符串类型(varchar),MySQL自动识别类型
select * ,1 as sex from student;//此时sex为int类型

在这里插入图片描述

MySQL添加值自动增长列

写法一:
set @rownum=0;
select @rownum:=@rownum+1 as num ,student.* from student;
写法二:
select @rownum:=@rownum+1 as num ,student.* from student,(select @rownum=0) t;

注意student.* 不能写成 * ,注意赋值是 ‘:=’

拓展:关于hive中的查询自增列

方式一:使用row_number() over() 实现。

猜你喜欢

转载自blog.csdn.net/weixin_43695091/article/details/89054346
今日推荐