• pgsql基本介绍



    join on 与数学原理
    pgsql切换数据库
    
    直接输入 C  youdatabasename  即可
    
    d 表名 —— 得到表结构
    
    select * from tablename查看表的数据
    

    相信有不少人读一遍两边都不懂,

    A集合有n行,x列,B集合有m行,y列   那么笛卡尔之积就是  一个(m*n)行,(x+y)列

    把复杂的问题简单化,把深奥的问题通俗化,那就是教育家,哈哈哈请叫我教育家:

    create table tbl_course(
        course_id bigint not null primary key,
        course_name varchar(12) not null
    );
    
    create table tbl_student(
        stu_id bigint not null,
        stu_name varchar(12),
        constraint pk_tbl_student_stu_id primary key(stu_id)
    );
    
    create table tbl_student_course(
        stu_id bigint not null,
        course_id bigint not null,
        constraint pk_tbl_student_course_stu_id_course_id primary key(stu_id,course_id),
        constraint fk_tbl_student_course_stu_id foreign key(stu_id) references tbl_student(stu_id) ,
        constraint fk_tbl_student_course_course_id foreign key(course_id) references tbl_course(course_id)
    );
    
    插入测试数据:
    insert into tbl_course values(1,'高等数学'),(2,'大学英语'),(3,'大学物理'),(4,'电影欣赏');
    
    insert into tbl_student values(1,'张三'),(2,'李四'),(3,'王五'),(4,'麻子');
    
    insert into tbl_student_course values (1,2),(1,4),(2,4),(3,4);
    
    select * from tbl_course ,tbl_student,tbl_student_course;//笛卡尔之积
    没有什么意义,因为只有四条数据,除非加上分组,去重才会有意义
    
    二.内连接
    JOIN连接分为内连接和外连接,而外连接又分为左外连接,右外连接,全外连接。

    内连接只要中间那一部分
    
    三.左外连接
    
    左外连接其实是一个内连接然后加上左表独有的数据行,结果集中右表的字段自动补充NULL。
    
    LEFT OUTTER JOIN ,其中OUTER可以省略。


    select * from tbl_student left join tbl_student_course using(stu_id) left join tbl_course using(course_id);
    //以左边学生为准,包括未选课的4号学生麻子

    四.右外连接
    右外连接其实是一个内连接然后加上右表独有的数据行,结果集中左表的字段自动补充NULL。
    
    RIGHT OUTTER JOIN ,其中OUTER可以省略。
    //以右边的表为准,左边不足的为空,和mysql很相似



    
    
    五.全外连接
    全外连接其实是一个内连接然后加上左表和右表独有的数据行,左表独有的数据行右表的字段补充NULL,右表独有的数据行左表字段补充NULL。

















    查询没有选课的学生:
    
    
    
    
    

     

    查询只在右表中存在的数据
    查找没有被选择的课程




    
    
    查询没有选课的学生和没有被选的课程

    就是三表联查 选择彼此都没有的那部分
    
    

    一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
  • 相关阅读:
    [转]Extundelete--数据恢复软件
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/wangbiaohistory/p/14726761.html
Copyright © 2020-2023  润新知