zoukankan      html  css  js  c++  java
  • 排序与连表查询

    排序

    排序规则

    # order by 主排序字段 [asc|desc], 次排序字段1 [asc|desc], ...次排序字段n [asc|desc]
    

    未分组状态下

    mysql>: select * from emp;
    
    # 按年龄升序
    mysql>: select * from emp order by age asc;
    # 按薪资降序
    mysql>: select * from emp order by salary desc;
    
    # 按薪资降序,如果相同,再按年龄降序
    mysql>: select * from emp order by salary desc, age desc;
    # 按龄降序,如果相同,再按薪资降序
    mysql>: select * from emp order by age desc, salary desc;
    

    分组状态下

    mysql>:
    select 
    	dep 部门,
    	group_concat(name) 成员,
    	max(salary) 最高薪资,
    	min(salary) 最低薪资,
    	avg(salary) 平均薪资,
    	sum(salary) 总薪资,
    	count(gender) 人数
    from emp group by dep;
    
    # 最高薪资降序
    mysql:
    select 
    	dep 部门,
    	group_concat(name) 成员,
    	max(salary) 最高薪资,
    	min(salary) 最低薪资,
    	avg(salary) 平均薪资,
    	sum(salary) 总薪资,
    	count(gender) 人数
    from emp group by dep
    order by 最高薪资 desc;
    

    限制 limit

    # 语法:limit 条数  |  limit 偏移量,条数
    mysql>: select name, salary from emp where salary<8 order by salary desc limit 1;
    
    mysql>: select * from emp limit 5,3;  # 先偏移5条满足条件的记录,再查询3条
    

    连表查询

    连接

    # 连接:将有联系的多张表通过关联(有联系就行,不一定是外键)字段,进行连接,形参一张大表
    # 连表查询:在大表的基础上进行查询,就称之为连表查询
    
    # 将表与表建立连接的方式有四种:内连接、左连接、右连接、全连接
    

    一对多数据准备

    mysql>: create database db3;
    mysql>: use db3;
    
    mysql>: 
    create table dep(
    	id int primary key auto_increment,
    	name varchar(16),
    	work varchar(16)
    );
    create table emp(
    	id int primary key auto_increment,
    	name varchar(16),
    	salary float,
    	dep_id int
    );
    insert into dep values(1, '市场部', '销售'), (2, '教学部', '授课'), (3, '管理部', '开车');
    insert into emp(name, salary, dep_id) values('egon', 3.0, 2),('yanghuhu', 2.0, 2),('sanjiang', 10.0, 1),('owen', 88888.0, 2),('liujie', 8.0, 1),('yingjie', 1.2, 0);
    

    笛卡尔积

    # 笛卡尔积: 集合 X{a, b} * Y{o, p, q} => Z{{a, o}, {a, p}, {a, q}, {b, o}, {b, p}, {b, q}}
    
    mysql>: select * from emp, dep;
    
    # 总结:是两张表 记录的所有排列组合,数据没有利用价值
    

    内连接

    # 关键字:inner join on
    # 语法:from A表 inner join B表 on A表.关联字段=B表.关联字段
    
    mysql>: 
    select 
    	emp.id,emp.name,salary,dep.name,work 
    from emp inner join dep on emp.dep_id = dep.id 
    order by emp.id;
    
    # 总结:只保留两个表有关联的数据
    

    左连接

    # 关键字:left join on
    # 语法:from 左表 left join 右表 on 左表.关联字段=右表.关联字段
    
    mysql>: 
    select 
    	emp.id,emp.name,salary,dep.name,work 
    from emp left join dep on emp.dep_id = dep.id 
    order by emp.id;
    
    # 总结:保留左表的全部数据,右表有对应数据直接连表显示,没有对应关系空填充
    

    右连接

    # 关键字:right join on
    # 语法:from A表 right join B表 on A表.关联字段=B表关联字段
    
    mysql>: 
    select 
    	emp.id,emp.name,salary,dep.name,work 
    from emp right join dep on emp.dep_id = dep.id 
    order by emp.id;
    
    # 总结:保留右表的全部数据,左表有对应数据直接连表显示,没有对应关系空填充
    

    左右可以相互转化

    mysql>: 
    select 
    	emp.id,emp.name,salary,dep.name,work 
    from emp right join dep on emp.dep_id = dep.id 
    order by emp.id;
    
    mysql>: 
    select 
    	emp.id,emp.name,salary,dep.name,work 
    from dep left join emp on emp.dep_id = dep.id 
    order by emp.id;
    
    # 总结:更换一下左右表的位置,相对应更换左右连接关键字,结果相同
    

    全连接

    mysql>: 
    select 
    	emp.id,emp.name,salary,dep.name,work 
    from emp left join dep on emp.dep_id = dep.id 
    
    union
    
    select 
    	emp.id,emp.name,salary,dep.name,work 
    from emp right join dep on emp.dep_id = dep.id 
    
    order by id;
    
    # 总结:左表右表数据都被保留,彼此有对应关系正常显示,彼此没有对应关系均空填充对方
    

    一对一与一对多情况一致

    # 创建一对一 作者与作者详情 表
    create table author(
    	id int,
        name varchar(64),
        detail_id int
    );
    create table author_detail(
    	id int,
        phone varchar(11)
    );
    # 填充数据
    insert into author values(1, 'Bob', 1), (2, 'Tom', 2), (3, 'ruakei', 0);
    insert into author_detail values(1, '13344556677'), (2, '14466779988'), (3, '12344332255');
    
    # 内连
    select author.id,name,phone from author join author_detail on author.detail_id = author_detail.id order by author.id;
    
    # 全连
    select author.id,name,phone from author left join author_detail on author.detail_id = author_detail.id
    union
    select author.id,name,phone from author right join author_detail on author.detail_id = author_detail.id
    order by id;
    

    多对多:两表两表建立连接

    # 在一对一基础上,建立 作者与书 的多对多关系关系
    
    # 利用之前的作者表
    create table author(
    	id int,
        name varchar(64),
        detail_id int
    );
    insert into author values(1, 'Bob', 1), (2, 'Tom', 2), (3, 'ruakei', 0);
    
    # 创建新的书表
    create table book(
    	id int,
        name varchar(64),
        price decimal(5,2)
    );
    insert into book values(1, 'python', 3.66), (2, 'Linux', 2.66), (3, 'Go', 4.66);
    
    # 创建 作者与书 的关系表
    create table author_book(
    	id int,
        author_id int,
        book_id int
    );
    # 数据:author-book:1-1,2  2-2,3  3-1,3
    insert into author_book values(1,1,1),(2,1,2),(3,2,2),(4,2,3),(5,3,1),(6,3,3);
    
    # 将有关联的表一一建立连接,查询所以自己所需字段
    select book.name, book.price, author.name, author_detail.phone from book 
    join author_book on book.id = author_book.book_id
    join author on author_book.author_id = author.id
    left join author_detail on author.detail_id = author_detail.id;
    
  • 相关阅读:
    11.14 mii-tool:管理网络接口的状态
    11.15 dmidecode:查询系统硬件信息
    11.11 ntsysv:管理开机服务
    HDU 2476 String painter 刷字符串(区间DP)
    HDU 1085 Holding Bin-Laden Captive! 活捉本拉登(普通型母函数)
    母函数的应用
    HDU 1028 Ignatius and the Princess III伊格和公主III(AC代码)母函数
    HDU 1059 Dividing 分配(多重背包,母函数)
    HDU 2955 Robberies抢劫案(01背包,变形)
    HDU 1011 Starship Troopers星河战队(树形dp)
  • 原文地址:https://www.cnblogs.com/aden668/p/11587521.html
Copyright © 2011-2022 走看看