as and distinct keywords

1. as keyword

When using SQL statements to display results, often the field names displayed on the screen do not have good readability. At this time, you can use as to give the field an alias.

  1. Use as to alias a field

    select id as 序号, name as 名字, gender as 性别 from students;
    
  2. 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;
    

    Description:

         It doesn't seem to make much sense to alias the table here, but it is not the case. We must alias the table when we learn self-join in the later stage.

 

2. The distinct keyword

Distinct can remove duplicate data rows.

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

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

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

 

3. Summary

  • The as keyword can give aliases to the fields or table names in the table
  • The distinct keyword can remove duplicate data rows.

Guess you like

Origin blog.csdn.net/weixin_48135624/article/details/115231571