zoukankan      html  css  js  c++  java
  • mysql联合查询

    班级表:
    CREATE TABLE `join_test_class` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` char(15) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
    
    学生表:
    CREATE TABLE `join_test_student` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` char(15) DEFAULT NULL,
      `cla_id` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
    
    • 查询某个学生的班级
    mysql> select a.*,b.* from join_test_student as a left join join_test_class as b on a.cla_id = b.id where a.id = 2;
    +----+-------+--------+----+---------+
    | id | name  | cla_id | id | name    |
    +----+-------+--------+----+---------+
    |  2 | name2 |      2 |  2 | 班级2   |
    +----+-------+--------+----+---------+
    1 row in set (0.00 sec)
    
    • 查询某个班级的学生信息
    mysql> select a.*,b.* from join_test_student as a left join join_test_class as b on a.cla_id = b.id where b.id = 3;
    +----+-------+--------+----+---------+
    | id | name  | cla_id | id | name    |
    +----+-------+--------+----+---------+
    |  3 | name3 |      3 |  3 | 班级3   |
    +----+-------+--------+----+---------+
    1 row in set (0.00 sec)
    
    • 查询所有学生里对应的班级cla_id无效
    mysql> select a.*,b.* from join_test_student as a left join join_test_class as b on a.cla_id = b.id where b.id is null;
    +----+-------+--------+------+------+
    | id | name  | cla_id | id   | name |
    +----+-------+--------+------+------+
    |  4 | name4 |      4 | NULL | NULL |
    +----+-------+--------+------+------+
    1 row in set (0.00 sec)
    
    • 查询所有班级里,没有学生的班级
    mysql> select a.*,b.* from join_test_student as a right join join_test_class as b on a.cla_id = b.id where a.id is null;
    +------+------+--------+----+---------+
    | id   | name | cla_id | id | name    |
    +------+------+--------+----+---------+
    | NULL | NULL |   NULL |  1 | 班级1   |
    +------+------+--------+----+---------+
    1 row in set (0.00 sec)
    
  • 相关阅读:
    Linux设置静态IP
    jenkins+findbugs
    CentOS 6.6 安装 Node.js
    未来物联网、人工智能无法迈过的技术是什么
    未来物联网、人工智能无法迈过的技术是什么
    spss-数据清洗-处理重复数据
    spss-数据清洗-处理重复数据
    大数据时代数据管理方式研究
    大数据时代数据管理方式研究
    Excel表格文本格式的数字和数字格式如何批量转换
  • 原文地址:https://www.cnblogs.com/wangweiwen/p/6560824.html
Copyright © 2011-2022 走看看