mysql数据库初学 二

八、单表查询table数据 select 用法:

  •                -------  注意 :  Null 为特殊值, 任意数值与Null运算都为 Null ---------

   1、普通表单查询:

     (1)查询表单每行所有内容 : *           

select * from smallbro;
select * from smallbro where id between 3 and 10;

     (2)查询每行指定字段内容:

select name,money from smallbro;   ------ 字段名与字段名用 , 隔开
select name,money from smallbro where id in (1,4,5,6);
select name,money from smallbro where money > 10000;

     (3)查询时赋予字段别名:

select name as 姓名,money as 保护费 from smallbro;        ------- 赋予的别名不需要加 “ ”
select name 姓名,money 保护费 from smallbro;   -------- as 可以省略

     (4)查询时去重:distinct

select distinct name,sex,age,money from smallbro;

     (5)查询时字段进行预算:

select name,money-200 as money  from smallbro;
select name,money%10 as money  from smallbro;

     (6)合并多类数据:concat() 函数   -------- 字符串拼接

                  注意:

                     例:select concat("我是:",name,",  我今天收了",money,"元") as 日报 from smallbro;

 2、表单条件查询:where  、like关键字 .......

    (1)逻辑,关系运算符:

                    https://blog.csdn.net/qq_16555103/article/details/86011130#t44  ----------- 第六节

   (2)判断是否为空值 :is null 、is not null

select * from smallbro where money is not Null; // 判断非空的
select * from smallbro where money is Null;     //判断为空的

   (3)like 关键字 ------- 用于模糊匹配:

查看名字里带 '渣' 的:
select * from smallbro where name like '%渣%';
查看名字是以 '渣' 开头的:
select * from smallbro where name like '渣%';
查看名字 第二个字 为渣的:
select * from smallbro where name like '_渣%';

   (4)排序查询:order by

根据保护费 排序  // asc 升序 从小到大  desc 降序 从大到小:
select * from smallbro order by money;  --------- 默认是升序,asc
select * from smallbro order by money desc;

                ① 多个字段 order by ,排序执行从左到右: ---------- 目的:重名排序

                 ② order by 支持 字段运算排序:

   3、表单查询统计函数:

     (1)计数函数:count() 

                    ① 计算 表格 数据的行数: *  该种方式可以实现对表中记录进行统计,不管表字段中包含的是NULL值还是非NULL值。

select count(*) as 总行数 from smallbro;

                   ② 计算 表格 数据某字段 的 数据个数:该种方式可以实现对指定字段的记录进行统计,在具体统计时将忽略NUll值。

                               特点:count() 不会计数 null值 

select count(money) as money字段非null个数 from smallbro;    

      (2)求和函数 sum() 、平均数 avg()、最大值max() 、最小值min()

select sum(money) as money字段数据总值 from smallbro;
select max(money) as money字段最大值 from smallbro;    

         (3) 计算字符个数char_length()、字节个数length():

select * from staff where char_length(name) =3; -----姓名字符数等于3
select * from staff where length(name) =3; ---姓名字节数等于3(一个中文3个字节)

 4、分组查询:group by   

  •                   特点: 分组字段一般会有至少有两行数据该字段值相同,即一行数数据分为一组没有实际意义。      

                        用法: select 查询字段名 from table名 group by 分组字段名;

select prup_id as 分组的字段,sum(money) from smallbro group by grup_id;
   // 对 grup_id 相同的数据进行分为一组,求出每组数据中money字段的数据之和。

    (1)分组的数据条件筛选:having 

                 用途:having 用于条件筛选分组后的数据,常用于 group by 中,group by 条件判断不能用关键字where。 

                 特点:

  •                           having执行顺序: 第一步:group by 分组 ;第二步:计算选择字段;第三步:having 对分组计算后的数据
  •                                                         进行筛选。
  •                           where 执行顺序:where 用于对整个表 进行时筛选,它常放在group by 语句前。

    (2)group by 可以对多个字段自行分组:

              理解:每一个字段分组后,单独每个组继续分组

通过导师分组,并且要查看每个导师下男女 的 总钱数:
select big_id,sex,sum(money) from teachers group by teacher,sex;

    (3)group by 查询未分组字段:

              group by select查询的特点:默认值只会查询出 用来分组的 字段,否者将会报错  

            解决方法:

                1) 使用函数 group_concat() :

                      函数 group_concat() 会把 分组过后一组的某个字段所有信息显示出来:

select sex,group_concat(money) from smallbro group by sex;   // 注意group_concat 与 聚合函数的区别
select sex,sum(money) from smallbro group by sex;

               2)在mysql目录中配置文件my.ini中的[mysqld]下添加:

                    第一步:添加

sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

                    第二步:重启 mysql 服务即可              
                               net stop mysql
                               net start mysql

              注意:添加成功后  这些字段只会每组的该字段的第一个数据

5、限制查询数量:limit(分页)

                       用法:imit start,num
                               ----     start 代表起始值,从0开始
                               ----     num 代表取得条数

查询前 50条 
select jobName,salary from zhilian_data limit 0,50;
查询 51-100条
select jobName,salary from zhilian_data limit 50,50;

6、单表查询的例题:

              https://blog.csdn.net/qq_16555103/article/details/86255056

九、多表查询table数据 select 用法:

  1、多表查询的基本知识:

    (1)笛卡尔乘积:

                 多表连接查询的时候,如果没有加入对应的where / on条件,两张表数据相乘,作为返回的结果(数据表行数相乘,

                      列数相加)

      (2)去除笛卡尔乘积:

                 ① 把2张表关联关系作为where / on 条件

                 ② 子查询

   (3)多表查询与 外键约束的关系:

                 多表查询的本质是把多个表按照where / on的关系条件链接为一张虚拟表查询,它与外键约束没有必然的的联系,

                      外键约束只是方便 主从表自动更新删除。

2、from 多个表 构成 链表:

       用法: select  别名1.字段名,别名2.字段名 from table1 as 别名1,table2 as 别名2 where table1.某字段=table2.某字段

select bigbro.name,smallbro.name from bigbro,smallbro where bigbro.id=smallbro.big_id;

3、内链接方式:... join ...  on ... 

  •                      链接表的特点:①它是一张表,是由多张表 join  链接组成的,它的列字段是 所有的表 所有列字段拼接,它的
  •                                                  行数据数 是由 多张表 满足 on 条件字段 的数据行数。    
  •                                                ② 3个及3个以上表 join 链接时执行顺序:从前到后,依次拼接(第一个和第二个拼接
  •                                                        得到新表后 再 拼接第三个表 得到新表 再再 拼接 第四个表  ...... )

   (1) join 支持 多张表链接,如下:

select c.c_id 客户标识,c.name 客户名称,b.bank_name 银行名称,d.amount 存款 from 

    customer c join depositer d on d.c_id=c.c_id join bank b on b. b_id=d.b_id;

 4、左外链接 left join on

5、右外链接 right join on

          与左链接相反

6、union :合并mysql语句

            特点:将多条mysql 语句 的 结果 整合到 一张表中。

  (1)union :去除重复的 结果

              用法: SELECT field1 field2 ...fieldn FROM tablename1  UNION | UNION ALL

                          SELECT field1 field2 ...fieldn FROM tablename2  UNION | UNION ALL

                          SELECT field1 field2 ...fieldn FROM tablename3……

  (2)union all:保留重复的结果

7、子查询:

  •         特点:① 子查询 的 本质 是嵌套 ,将一个表的查询结果(需要起 一个别名) 嵌套到 别的语句当中,通常
  •                         可用 到 子查询更新、删除、多链表(join)查询查询数据。
  •                    ② 子查询在 应用于 多表链接(join)时,由于子查询的表 数据量小 ,因此做 链表查询时将大大 减
  •                         小笛卡尔积 。

 (1)子查询结果是 多行单列:

  •          该子査询语句一般(不是绝对)会在主査询语句的WHERE子句里出现,通常会包含IN、ANYALLEXISTS
  •          等关键字

           ① in  的用法:  

                       注意: 在数据为一行 一列时,in 等价于 = 

1.使用子查询将数据表deposite中刘德华的存款金额加10000
 update depositer set amount=amount+10000 where c_id = (select c_id from customer where name ='刘德华');
      // 用 in的方式 
update depositer set amount=amount+10000 where c_id in (select c_id from customer where name ='刘德华');

          ② 带有关键字ANY的子査询

        ③ 带有关键字ALL的子查询

(2)子查询结果为多行多列:使用 join .. on ...

  •             ① 当子查询的返回结果为多行多列数据记录时,该子査询语句一般会在主查询语句的from子句里,被当作一张临时表的
  •                 方式来处理
  •             ②  临时表需要起一个 别名。

         下一节 :https://blog.csdn.net/qq_16555103/article/details/86483083  

猜你喜欢

转载自blog.csdn.net/qq_16555103/article/details/86181736