一/表单内复杂查询
limit #取前若干条 order by 字段名 asc/desc #按照字段名升序或降序排列 like '%西%' #判断字符串是否包含西
逻辑运算符: in 判断结果是否落在指定范围内 between 判断是否落在某个区间 and or
分组及分组后的筛选: group by 字段名 #按照字段名分组 having #分组后的筛选条件 PS:where是分组前的条件筛选
数据库函数: count(col) max(col),min(col) avg(col) sum(col)
二/多表联合查询
1.子表A union 子表B #整合字段数量相同的子查询
2.join:
* A (inner) join() B 查询范围:为左右二表的交集
* A left join() B 查询范围包含:交集+左表独有部分
* A right join() B 查询范围:交集+右表独有部分
三/表关系管理
外键管理:
一般来说都是从表应引用主表的字段,外键定义在从表中,因为一旦从表删除,外键也一并删除了,并不会给主表留下脏数据
定义外键:
foreign key(从表字段) references 主表(主表字段) 例: foreign key(classid) references clazz(id)追加外键:
方法一 alter table 从表 add foregin key 从表(从表字段) references 主表(主表字段); 例: alter table student add foregin key temp(classid) references clazz(id); 方法二: alter table 从表 add constraint 外键名称 foreign key 从表(从表字段) references 主表(主表字段);
删除外键:
show craete table student; //据此找出classid对应的外键名称: alter table student drop foreign key 外键名称; //删除外键 alter table student drop classid;
删除外键定义:
show create table student; 据此找出classid对应的外键名称 alter table student drop foreign key student_ibfk_1; 删除这个奇奇怪怪的外键名称 alter table student drop classid;