create table scores(
id int primary key auto_increment,
stuid int,
subid int,
score decimal(5,2)
);
alter table scores add constraint stu_sco foreign key(stuid) references students(id);
create table scores(
id int primary key auto_increment,
stuid int,
subid int,
score decimal(5,2),
foreign key(stuid) references students(id),
foreign key(subid) references subjects(id)
);
alter table scores add constraint stu_sco foreign key(stuid) references students(id) on delete cascade;
select students.sname,subjects.stitle,scores.score
from scores
inner join students on scores.stuid=students.id
inner join subjects on scores.subid=subjects.id;
select students.sname,avg(scores.score)
from scores
inner join students on scores.stuid=students.id
group by students.sname;
select students.sname,avg(scores.score)
from scores
inner join students on scores.stuid=students.id
where students.gender=1
group by students.sname;
select subjects.stitle,avg(scores.score)
from scores
inner join subjects on scores.subid=subjects.id
group by subjects.stitle;
select subjects.stitle,avg(scores.score),max(scores.score)
from scores
inner join subjects on scores.subid=subjects.id
where subjects.isdelete=0
group by subjects.stitle;
create table areas(
id int primary key,
atitle varchar(20),
pid int,
foreign key(pid) references areas(id)
);
source areas.sql;
select city.* from areas as city
inner join areas as province on city.pid=province.id
where province.atitle=‘山西省‘;
select dis.*,dis2.* from areas as dis
inner join areas as city on city.id=dis.pid
left join areas as dis2 on dis.id=dis2.pid
where city.atitle=‘广州市‘;
select ascii(‘a‘);
select char(97);
select concat(12,34,‘ab‘);
select length(‘abc‘);
select substring(‘abc123‘,2,3);
select trim(‘ bar ‘);
select trim(leading ‘x‘ FROM ‘xxxbarxxx‘);
select trim(both ‘x‘ FROM ‘xxxbarxxx‘);
select trim(trailing ‘x‘ FROM ‘xxxbarxxx‘);
select space(10);
select replace(‘abc123‘,‘123‘,‘def‘);
select lower(‘aBcD‘);
select abs(-32);
select mod(10,3);
select 10%3;
select floor(2.3);
select ceiling(2.3);
select round(1.6);
select pow(2,3);
select PI();
select rand();
select year(‘2016-12-21‘);
select ‘2016-12-21‘+interval 1 day;
日期格式化date_format(date,format),format参数可用的值如下
获取年%Y,返回4位的整数
* 获取年%y,返回2位的整数
* 获取月%m,值为1-12的整数
获取日%d,返回整数
* 获取时%H,值为0-23的整数
* 获取时%h,值为1-12的整数
* 获取分%i,值为0-59的整数
* 获取秒%s,值为0-59的整数
select date_format(‘2016-12-21‘,‘%Y %m %d‘);
select current_date();
select current_time();
select now();
create view stuscore as
select students.*,scores.score from scores
inner join students on scores.stuid=students.id;
select * from stuscore;
show create table students;
alter table ‘表名‘ engine=innodb;
开启begin;
提交commit;
回滚rollback;
原文:https://www.cnblogs.com/luchun666/p/9383355.html