Use of as and distinct keywords in Mysql database

Use of as and distinct keywords in Mysql database

1.as keyword
When using SQL statements to display results, the field names displayed on the screen are often not readable. At this time, you can use as to give the field an alias.

Use as to alias a field

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

You can alias the table through 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;

Explanation: It
doesn't seem to make much sense to alias the table here, but it is not the case. When we use self-join, we must alias the table.

2.distinct keyword
Distinct can remove duplicate data rows.

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

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

-- 看到了很多重复数据 想要对其中重复数据行进行去重操作可以使用 distinct
select distinct name, gender from students;

Summary: The
as keyword can give an alias to a field or table name in the table. The
distinct keyword can remove duplicate data rows.

Guess you like

Origin blog.csdn.net/li944254211/article/details/109262572
Recommended