Mysql中as和distinct关键字

微信扫一扫,分享到朋友圈

Mysql中as和distinct关键字
收藏 00

as关键字

在使用SQL语句显示结果的时候,往往在屏幕显示的字段名并不具备良好的可读性,此时可以使用 as 给字段起一个别名。

使用 as 给字段起别名

select id as 序号, name as 名字, gender as 性别 from students;

可以通过 as 给表起别名

-- 如果是单表查询 可以省略表名
select id, name, gender from students;

-- 表名.字段名
select students.id,students.name,students.gender from students;

-- 可以通过 as 给表起别名 
select s.id,s.name,s.gender from students as s;

在这里给表起别名看起来并没有什么意义,然而并不是这样的,我们在后期学习 自连接 的时候,必须要对表起别名。

distinct关键字

distinct可以去除重复数据行。

select distinct 列1,... from 表名;

例: 查询班级中学生的性别
select name, gender from students;

-- 看到了很多重复数据 想要对其中重复数据行进行去重操作可以使用 distinct
select distinct name, gender from students;
  • as 关键字可以给表中字段 或者 表名起别名
  • distinct 关键字可以去除重复数据行。
一个热爱互联网的咸鱼

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片

热门

    抱歉,30天内未发布文章!
返回顶部