暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL基本语法手册(二)

南京的萝卜 2021-05-16
484

1.查询时起别名

    select name [as] 名字 from 表名
    select name [as] 名字 from student;

    2.查询时合并列

      select name, math, english, math+english [as] 总分 from student;

      3.查询时去除重复

        select distinct name from student;

        4.条件查询

        逻辑语句 and 和 or
        判断语句 = 和 in
          select * from student where math=50 or math=85;
          select * from student where math in (50, 85);

          5.比较条件(> < >= <= = <>(不等于))

            select * from student where math>50 and math<96;
            select * from student where math<>65;
            select * from student where math is null;

            6.模糊查询(like)

            % _ 占位符  %:为任意字符  _:占一个字符

              select * from student where name like '%红';
              select * from student where name like '_能';

              7.聚合函数

              将列作一个整体,进纵向的计算

                      1. count 计算个数(一般使用非空列表名或者count(*))

                      2.avg 计算平局数

                      3.min 最小值

                      4.max 最大值

                      5.sum 求和

                      注意:聚合函数的计算, 排除null的值

                      解决办法:1.选取非空的列表

                                    2.使用ifnull(列表名, 0)

                -- 求和
                select sum(math) from student;
                -- 求个数
                select count(*) from stduent;

                8.分组查询

                group by

                hangving 使用在group 的后面

                where 使用在group前

                  // 得出每个性别人的总数
                  Select gender, count as 总数 from class group by gender;
                  select gender, count(*) as 总数 from class where math > 60 group by gender having 总数 > 1;
                  9. 分页查询
                  limit      
                  分页公式:开始的索引 = (当前页码 - 1) *每页显示条数    
                    select * from class limit 0,3;
                    select * from class limit 3,3;
                    select * from class limit 6,3;
                       
                    10.级联操作
                      -- 添加级联操作
                      -- 语法:
                      alter table 表名 add constraint 外键名
                      foreign key (外键关键字) references 表名(列表名) on update cascade;
                      -- 级联分类
                      1. on update cascade; 更新级联
                      2. on delete cascade; 删除级联

                      文章转载自南京的萝卜,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                      评论