1、联表查询left join 的使用
假如有两张表,一张学生信息表 students,一张学期成绩表results,
那么把第一学期的学生总成绩,按照从高到低排序,取前十名怎么来写呢
select r.students_id,sum(score) nums from results r left join students s on r.students_id=s.students_id where r.semester ='1' group by r.students_id order by nums desc limit 10;
2、复制表结构的方法
create table table2 like table1;
还有一种方法是
create table table2 select * from table1 where 1=2;
3、复制数据的方法
insert into table2 select * from table1;
4、一次性复制表结构和表数据的方法
create table table2 select * from table1;
5、更改表名同时迁移数据
alter table table2 rename to table_bak;
6、查看表结构
desc table1;
另一种查看DDL的方法是
show create table table1;
7、修改表字段类型
alter table table1 modify column fenshu decimal(10,2);
8、myisam如何清理碎片
mysql在使用myisam存储引擎的时候会产生碎片,我们在清理了数据库数据之后,数据库操作仍然很慢,那么需要清理一下碎片
optimize table table1;