• 数据库的连接查询


    首先创建表,并在表中插入数据

    --创建学生表
    create table students (
    id int unsigned not null auto_increment primary key,
    name varchar(20) default '',
    age tinyint unsigned default 0,
    high decimal(5,2),
    gender enum('男', '女', '中性', '保密') default '保密',
    cls_id int unsigned default 0,
    is_delete bit default 0
    );

    --创建班级表
    create table classes(
    id int unsigned auto_increment primary key not null,
    name varchar(20) not null
    );

    --往students表里插入数据
    insert into students values
    (0,'小明',18,180.00,2,1,0),
    (0,'小月月',19,180.00,2,2,0),
    (0,'彭于晏',28,185.00,1,1,0),
    (0,'刘德华',58,175.00,1,2,0),
    (0,'黄蓉',108,160.00,2,1,0),
    (0,'凤姐',44,150.00,4,2,1),
    (0,'王祖贤',52,170.00,2,1,1),
    (0,'周杰伦儿',34,null,1,1,0),
    (0,'程坤',44,181.00,1,2,0),
    (0,'和珅',55,166.00,2,2,0),
    (0,'刘亦菲',29,162.00,3,3,0),
    (0,'金星',45,180.00,2,4,0),
    (0,'静香',18,170.00,1,4,0),
    (0,'郭静',22,167.00,2,5,0),
    (0,'周杰',33,178.00,1,1,0),
    (0,'钱小豪',56,178.00,1,1,0),
    (0,'谢霆锋',38,175.00,1,1,0);


    --向classes表里插入数据
    insert into classes values (0, '云唯_01期'),(0, '云唯_02期');

    对上面的数据进行查询实验:

    inner join

    内关联,连接查询:多个表连接起来查询,查询结果多个表的交集

    left join
    左连接,以左面的为基准,左边有但是右边没有提供可匹配的,左边没可匹配项的显示null
    right join
    右链接,以右边的为准,右边有但是左边没有匹配的,右边没有可匹配项的显示null
    自关联
    自己关联自己,在一个表中,
    并且数据结构都一样,查询时需要给表七别名,否则会出错。

    -- 连接查询
    -- inner join ... on
    -- 两个表连接查询
    select * from students inner join classes
    -- 查询能够对应班级的学生以及班级信息
    select * from students inner join classes on students.cls_id=classes.id;
    -- 按照要求显示姓名,班级
    select students.*, classes.name from students inner join classes on students.cls_id=classes.id;
    --给数据表起别名
    select s.name, c.name from students as s inner join classes as c on s.cls_id=c.id;
    -- 在以上查询中将班级姓名显示在第一列
    select classes.name,students.* from students inner join classes on students.cls_id=classes.id;
    -- 查询有能够对应班级的学生以及班级信息,按照班级进行排序
    select classes.id, students.* from students inner join classes on students.cls_id=classes.id order by classes.id;
    -- 当是同一个班级的时候,按照学生的id从小到大
    select classes.id, students.* from students inner join classes on students.cls_id=classes.id order by classes.id, students.id;

    -- 自关联
    create table areas(
    aid int primary key auto_increment,
    name varchar(20),
    pid int
    );
    -- 查询出河北省所有市

    --需要起别名
    select * from areas as province inner join areas as city on province.aid=city.pid having name='河北省';

    select province.name,city.name from areas as province inner join areas as city on province.aid=city.pid having province.name='河北省';

    -- 子查询
    -- 标量子查询
    -- 查询出北京市所有区的信息
    select * from areas where pid=(select aid from areas where name='北京市');
    select * from areas where pid in (select aid from areas where name='北京市');

  • 相关阅读:
    2020.12.7
    IDEA修改代码后不用重新启动项目即可刷新
    期中测试人口普查登记题目
    Android去掉标题头
    Android限制输入框内容
    Android:setOnItemClickListener cannot be used with a spinner报错
    Android修改app图标
    将外部sqlite3数据库导入到Android项目中
    IDEA个人常用快捷键
    css选择器
  • 原文地址:https://www.cnblogs.com/dayle/p/9947146.html
Copyright © 2020-2023  润新知