mysql

1.常用数据类型

  int,char,varchar,double,float,text

2. group by和order by

  分组后的字段要么是分组字段或聚合函数

  where和having区别:

    where:在分组之前进行限定;后面不能跟聚合函数的判断

    having:在分组之后进行限定,如果不满足结果就不会显示出来;后面可以跟聚合函数判断

select user_name from t_user group by user_name order by id
select sex,avg(math) ,count(id) ID from student group by sex having ID>2;  --ID是别名

 3. 分页limit 页数,条数(页数从0开始)

  select * from person limit 0,3;从第一页开始,每页3条数据

  select * from person limit 3,3;第二页

  公式:开始索引=(当前页码-1)*每页条数

4. select  字段列表  from  表明列表  [where  条件 | group by  分组字段 |  order by  分组后的条件 |  limit  分页限定];

5. 连接join

(inner) join:

select * from tb1 t1 inner join tb2 t2 on t1.author=t2.author;  --匹配显示有相同author的字段

left join: 以左表为基表,左表字段数据会全部显示,另一个表没有匹配的字段会显示为空null

select * from tb1 t1 left join tb2 t2 on t1.author = t2.author; 

right join: 以右表为基表,右表的表字段数据会全部显示,另一个表中若没有匹配的字段会显示为空null

select * from tb1 t1 right join tb2 t2 on t1.author = t2.author;

6. alter修改表结构

  show columns from 表名;显示表字段

  alter table 表名 drop 字段;删除某个字段

  alter table 表名 add 字段;添加一个字段

  alter table 表名 modify 字段  类型;修改字段类型

  alter table 表名 change 旧字段 新字段 类型;修改字段类型及名称

  alter table 表名 modify 字段 类型 not null default 100;为字段添加默认值和非空

  alter table 表名 alter 字段 set default 1000;为字段设置默认值

  alter table 表名 alter 字段 drop default;删除该字段的默认值

  alter table 表名 rename to 新表名;修改表名

7. 索引index

  创建索引:create index 索引名 on 表名(字段名)

  修改表结构添加索引:alter table 表名 add index 索引名(字段名)

  删除索引:drop index 索引名 on 表名;

  在创建表时候直接指定索引:create table mytable(id int not null,username varchar(10) not null,index[indexname] username(length));

原文地址:https://www.cnblogs.com/Difcipo/p/14016906.html