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)
    
  • 相关阅读:
    Django DTL模板语法中的过滤器
    Django DTL模板语法中的url反转
    Django DTL模板语法中定义变量
    Django DTL模板语法中的循环的笔记
    UOJ #310 黎明前的巧克力 FWT dp
    6.15 省选模拟赛 老魔杖 博弈论 SG函数
    luogu P4887 模板 莫队二次离线 莫队 离线
    一本通 高手训练 1788 爬山 dp 斜率 凸包
    luogu P5289 [十二省联考2019]皮配 背包
    6.10 省选模拟赛 小C的利是 高斯消元 矩阵行列式
  • 原文地址:https://www.cnblogs.com/wangweiwen/p/6560824.html
Copyright © 2011-2022 走看看