十一月二十五号

--自关联

--1  北京  null

--2  朝阳区 1

--3  房山区 1

--4  吉林省 null

--5  长春市 4

--6  吉林市 4

--空间换时间

--商品表

-- id name pic

--商品样式表

-- id pid style  pic

--给班级表哦增加主键

alter table my_class add primary

key(name);

--插入数据

insert into my_class values(

'python1907','B408';)

insert into my_class values(

'python1907','B407';)

insert into my_class values(

'python1907','B407';)

--冲突处理

on duplicate key update

--更新教室

room='B407';

insert into my_class values(

'python1903','B408';)

--主键冲突:替换

replace into my_class values('python1903','B406');

--复制创建表

create table my_copy like

my_class;

--删除主键

alter table my_copy drop primary

key;

--虫复制

insert into my_copy select *from

my_class;

insert into my_copy select *from

my_copy;

--更新部分B406变成A406

update my_copy set eoom ='A406'

where room ='B406' limit3;

--删除数据:限制记录数为5

deletefrom my_copy where room=

'B409' limit5;

--给学生表增加主题

aiter table my_student modify id

int primary key auto_increment;

--清空表,重置自增长

truncate my_atudent;

--以下三句有什么区别?

-- deletefrom 表名;

-- truncate表名;

-- drop table表名;

-- select选项

select *from my_copy;

selete all *from my_copy;

--去重

selete distinct *from my_copy;

--向学生表插入数据

insert into my_student

values(null,'bc20190001','张三','男'),

(null,'bc20190002','李四','男'),

(null,'bc20190003','王五','女'),

(null,'bc20190004','赵六','男'),

(null,'bc20190005','周七','男');

--字段别名

select id,

numberas 学号,

nameas 姓名,

sex性别 from my_student;

--多表数据源

select *from my_student,my_class;

--子查询

select *from (select *from

my_student)as s;

--增加age年龄和height身高字段

alter table my_student add age

tinyint unsgned;

alter table my_student add height

tinyint unsigned;

--

增加字段值:rand取得一个0-1之间的随机数,floor向下取整

update my_student set age=flooe(

rand()*20+20), height=floor(rand

()20+170),

--找学生id为1,3,5的学生

select *from my_student where id

=1 || id=3 || id=5; --逻辑判断

select *from my_student where id

in(1,3,5); -- in表示在集合中

--找出身高在180到190之间的学生

select *from my_student where

height>=180 and height<=190;

select *from my_student where

height between180 and 190;

select *from my_student where

height between190 and 180;

--不成立,相当于height>=190 and

height<=108

select *from my_student where1;

--所有条件都满足

--根据性别分组

select *from my_student group by

sex;

--

分组统计:身高高矮,平均年龄,总年龄

select sex,count(*),max(height),

min(height),avg(age),sum(age)

from my_student group by sex;

--修改id为4的记录,把年龄置办为NULL

update my_student set age=null

where id=4;

select sex,count(*),count(age),

max(height),min(height),avg(age),

sum(age)from my_student group by

sex;

--修改id为1的记录,把性别置为女

update my_student set sex='女'

where id=1;

-- nan

-- nv

select sex,count(*)count(age),

max(height),min(height),avg(age),

sum(age)from my_student group by

sex desc;

--删除班级表原主键

alter table my_class drop primary

key;

--给班级表增加主键

alter table my_class add id int

primary key auto_increment;

--给学生表增加班级ID

alter table my_student add c_id

int;

update my_student set c_id=ceil(rand()*3);

--多字段分组:先班组,后男女

select c_id,sex,count(*)from

my_student group by c_id,sex;

--多字符排序

--统计

select c_id,count(*)from

my_student group by c_id;

--回溯统计

select c_id,count(*)from

my_student group by c_idwith

rollup;

--多字符分组回溯统计

select c_id,sex,count(*),

group_concat(name)from

my_student group by c_id,sex;

--多字段排序

select c_id,sex,count(*),

group_concat(name)from

my_student group by c_id,sex;

--多字段排序

elect c_id,sex,count(*),

group_concat(name)from

my_student group by c_id,sexwith

    rollup;

--

求出所有班级人数大于等于2的学生人数

select c_id,count(*)from

my_student group by c_id having

count(*)>=2;

select c_id,count(*)from

my_student where count(*)>=2

group by c_id;  --错误

select c_id,count(*)as total

frommy_student group by c_id having

total>=2

select c_id,count(*)astotal

from my_student where count(*)>=2

group by c_id;  --错误

-- having子句进行条件查询

select nameas 名字,numberas

学号 from my_student having名字

like'张%';

--排序

select *from my_student group by

c_id; --分组,为了进行统计

select *from my_student order by

c_id; --排序

--

多字段排序:先班级排序,后性别排序

select *from my-student order by

c_id,sex desc;

--查询学生:前两个

select *from my_student limit2;

select *from my_student limit0,

2; --记录数是从0开始编号

select *from my_student limit2,

2;

select *from my_student limit4,

2;

--更改ID为班级表的第一列

alter table my_class change id id

int first;

--交叉连接

select *from my-student cross

join my_class; -- mu_student

cross join my_class是数据源

--内连接

select *from my_student inner

join my_class on my_student.c_id=

my_class.id;

select *from my_student inner

join my_class on c_id=my_class.id;

select *from my_student inner

join my_class on c_id=id; --

错误,因为两张表都有id字段

--字段和表别名

select s.*,nameas c_name, c.roomfrom my_studentas s

inner join my_classas c

on s.c_id=c_id;

--

把学生表id为5的记录的c_id设置为NULL

update my_student set c_id=null

where id=5;

-- where代替on

select s.*,c.nameas c_name,c.

room --字段别名

from my_studentas s --表别名

inner join my_classas c

where s.c_id=c_id;

--左连接

select s.*,c.nameas c_name,c.

room --字段别名

from my_studentas s

left join my_classas c --

左表为主表:最终记录数至少不少于左表己有的记录数

on s.c_id=c.id;

--右连接

select s.*,c.nameas c_name,c.

room --字段别名

from my_studentas s

height join my_classas c --

右表为主表:最终记录数至少不少于右表己有的记录数

on s.c_id=c_id;

select s.*,c.nameas c_name,c.room

--字段别名

from my_classas c

right join my_studentas s

on s.c_id=c_id;

--自然内连接

select *from my_student natural

join my_class;

--修改班级表的name字段名为c_name

alter table my_class change name

c_name varchar(20)not null;

--自然左外连接

select *from my_student natural

left join my_class;

--外连接模拟自然外连接:using

select *from my_student left

join my_class using(id;

你可能感兴趣的:(十一月二十五号)