Data Manipulation - Query

Data Manipulation - Query

Create a data table

drop table if exists students;
create table students (
  studentNo varchar(10) primary key,
  name varchar(10),
  sex varchar(1),
  hometown varchar(20),
  age tinyint(4),
  class varchar(10),
  card varchar(20)
)

Prepare data

insert into students values
('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),
('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),
('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),
('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
('008', '小乔', '女', '河南', '15', '3班', null),
('009', '百里守约', '男', '湖南', '21', '1班', ''),
('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')
  • Queries all fields
select * from 表名
例:
select * from students
  • Query the specified field
  • In the latter part of the column name select, can be used as column aliases, the alias appear in the result set
select 列1,列2,... from 表名

-- 表名.字段名
select students.name,students.age from students

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

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

- 使用as给字段起别名
select studentNo as 学号,name as 名字,sex as 性别 from students

Eliminating duplicate rows

  • Before using the column select distinct rear eliminates duplicate rows
select distinct 列1,... from 表名;
例:
select distinct sex from students;
Published 240 original articles · won praise 77 · views 80000 +

Guess you like

Origin blog.csdn.net/dpl12/article/details/104196874