01-基本查询

1.查询所有字段

select * from 表名;
例:
select * from students;

2.查询指定字段

select 列1,列2,... from 表名;
例:
select name from students;

3.使用 as 给字段起别名

语法:select 原始字段名1 as 别名1, 原始字段名2 as 别名2 ..... from 表 where 条件(条件要写原始字段名);  

例:  
select userid as id, username as name from student where userid=1;  
注意:上述条件where userid =1 要用原始字段名,否则报错

4.可以通过 as 给表起别名

select s.id,s.name,s.gender from students as s;  

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

5.别名注意事项

  • 表的别名只在执行查询的时候使用,并不在返回结果中显示

  • 列别名定义之后,将返回给客户端显示,显示的结果字段为字段列的别名

6.消除重复行(加在列名前面,表示根据某个字段消除重复行)

在select后面列前使用distinct可以消除重复的行
select distinct 列1,... from 表名;
例:
select distinct gender from students;

Last updated

Was this helpful?