python—day39、单表查询、多表查询

 一:单表查询:

  1、以数字形式查看网络的端口号;

  netstat -an |findstr 3306

  2、设置分组查询模式

  set global sql_mode="strict_trans_tables,only_full_group_by";

  3、group by分组条件

  4、聚合函数:

    1.max 最大值

    2.min 最小值

    3.avg 平均值

    4.sum 和值

    5.count 个数值

  5、group_concat(分组之后用)

  6、concat(不分组之后用)

  7、having 过滤

    having和where 语法一样,不过having是在分组之后进行的进一步过滤。

    where不能用聚合函数,having可以;

    注意:

      having必须用在group by后面;

  8、order by 排序   默认升序

    order by asc;升序

    order by desc; 降序

  9、执行顺序

     select * from emp where id = 1 group by post order by desc;

     select * from emp group by post having id >1 order by desc;

    from ===> where ===> group by ===> having ===> order by ===>select ;

  10、语法顺序:

    select distinct 查询字段1,字段2from 表名 

    where 分组前的条件

    group by 分组依据

    having 分组后的过滤条件

    order by 排序字段

    limit 显示的余数

二:多表查询

  1、内连接:只取两张表有对应关系的记录练成一张虚拟表:

    select * from emp inner join dep on emp.dep_id = dep.id;

  2、左连接:在内连接的基础上,保留左边表没有对应关系的记录;

    slelect * from emp left join dep on emp.dep_id = dep.id;

  3、右连接:在内连接的基础上,保留右边表没有对应关系的记录;

    select * from emp right joib dep on emp.dep_id = dep.id;

  4、全连接:在内连接的基础上,保留左右表没有对应关系的记录;

    select * from emp left join dep on emp.dep_id = dep.id 

    union

    select * from emp right join dep on emp.dep_id = dep.id;

猜你喜欢

转载自www.cnblogs.com/kermitjam/p/9020299.html