zoukankan      html  css  js  c++  java
  • 我的学习之路_第十九章_SQL多表

    SQL语句

    【SQL查询语句】

    格式: select [distinct]*(所有)| 字段名,...字段名 from 表名 [where 条件过滤]

    -- 查询指定字段信息

    select 字段名 from 表名 ;

    -- 查询表中所有字段

    select * from 表名 ;

    -- 取出重复的记录

    select distinct 字段名 from 表名 ;

    -- 别名查询,使用as 关键字,关键字可以省略

    select 字段名 as 别名 from 表名 ;

    -- sql语句可以直接对列进行运算


    【条件查询】

    select [distinct]*(所有)| 字段名 ,...字段名 from 表名[where 条件过滤]

    比较运算符 : > < >= <= = <>(不等于) !=(不等于)

    逻辑运算符 : 与 and 或 or 非 not

    模糊查询 : like % : 任意字符 _: 表示单个字符

    -- 查询名字为某某的商品所有信息

    select * from 表名 where 字段名= '某某' ;

    select * from 表名 where 字段名 in ('某某') ;

    -- 查询价格为 800 的商品

    select * from 表名 where 字段名 = 800 ;

    -- 查询价格不是 800 的所有商品

    select * from 表名 where 字段名 != 800 ;

    select * from 表名 where 字段名<> 800 ;

    select * from 表名 where 字段名 not in (800) ; --了解

    select * from 表名 where not 字段名 in (800) ; -- 了解

    -- 查询价格大于 100的 所有商品信息

    select * from 表名 where 字段名> 100 ;

    -- 查询商品价格在 100 到 200 之间所有商品

    select * from 表名 where 字段名 >= 100 and 字段名 <= 200;

    -- 使用 between...and 进行改造 小的数值必须写在前边,可以多日期进行查询

    select * from 表名 where 字段名 between 100 and 200 ;

    -- 查询商品价格是200或者800的所有商品

    select * from 表名 where 字段名 = 200 or 字段名 = 800 ;

    -- 使用 in(多个字段) 改造

    select * from 表名 where 字段名 in(200,800);

    -- 查询以 '香'开头的所有商品

    select * from 表名 where 字段名 like '香%';

    -- 查询名字以'香'结尾的所有商品

    select * from 表名 where 字段名 like '%香';

    -- 查询名称含有'香'字的所有商品

    select * from 表名 where 字段名 like '%香%';

    -- 查询名称中是五个字的所有商品

    select * from 表名 where 字段名 like '_____';

    -- 查询第二个字为'香'的所有商品

    select * from 表名 where 字段名 like '_香%';

    -- 查询商品名称是null的值

    select * from 表名 where 字段名 is null ;

    -- 查询商品名称不是null的值

    select * from 表名 where 字段名 is not null ;

    select * from 表名 where not (字段名 is null) ;

    【排序查询】

    格式: select 字段| * from 表名 [where 条件过滤] [order by 字段[asc][desc]]

    升序 : asc 默认为升序
    降序 : desc

    注意 : 排序 order bt 要写在select 语句末尾

    -- 使用某一个字段排序(升序)

    select * from 表名 order by 字段名 asc ;

    select * from 表名 order by 字段名 ;

    -- 使用某一个字段排序(降序)

    select * from 表名 order by 字段名 desc ;

    -- 显示某一字段(去重复),并排序(降序)

    select distinct 字段名 from 表名 order by 字段名 desc ;

    -- 显示商品的价格大于 1000 的商品信息.并排序(降序)

    select * from 表名 where 字段名>1000 order by 字段名 desc;


    【聚合函数】

    对列进行操作,返回的结果是一个单一的值,忽略空值

    count : 统计指定列不为 null 的记录行数

    sum : 计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果是0;

    max : 计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运行;

    mix : 计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运行;

    avg : 计算指定列的平均值,如果指定类型不是数值类型,那么计算结果为0;

    格式: select 聚合函数(字段) from 表名 ;

    -- 查询表的总条数

    select count(*) from 表名 ;

    如果有空值那么不计算在内

    -- 查询价格大于 200 商品的总条数

    select count(*) from 表名 where 字段名>200 ;

    -- 对表查询,对某一列进行求和计算

    select sum(字段名) from 表名;

    如果是非数值字段求和,那么结果为0;

    -- 对商品表查询,对数字字段进行计算平均值

    select abg(字段名) from 表名;

    -- 统计某字段最大值和最小值

    select max(字段名) , mix (最小值) from 表名 ;

    【分组查询】

    select 被分组的字段 from 表名 group by 字段 [having 字段]

    注意: 被分组的字段,一定要写在 select 后面

    需求: 根据商品名称相同,对数据进行分组,对每组数据进行求和

    -- 对分组求和的结果进行过滤,只显示求和结果大于1000的商品

    /*
    where:只能在查询的过程中进行过滤,不能对结果过滤

    having : 对分组数据的查询结果,再次进行条件过滤

    */

    -- 查询出价格大于1000的商品,对大于1000的商品进行分组求和

    select 字段名 ,sum(字段名) 别名 from 表名 where 字段名 > 1000 group by 字段名 ;

    select 字段名,sum(字段名) 别名 from 表名 group by 字段名 having 别名>1000;

    select 字段名 ,sum(字段名) 别名 from 表名 group by 字段名 having sum(字段名)>1000;

    【分页查询】

    可以使用关键字limit m,n
    m : 可以变化页码 ,1,2,3,4,5
    n : 固定不变的分页数(每页有几条)
    数据库的数据是从0开始

    -- 只要前五条数据

    select * from 表名 limit 5;

    -- 要0开始 5 结束 的数据 (第一页的数据)

    select * from 表名 limit 0,5;

    -- 要6开始到10结束的数据

    select * from limit 5,5

    【多表的SQL语句】


    -- 创建一个表, 另一个表的主键作为外键

    create table 表名(

    id int primary key auto_increment,

    字段名,
    另一个表的主键 int
    );

    -- 把从表的字段 另一个表的主键 设置为主表的外键

    格式 : alter table 从表 add [constraint] [外键的名称] foreign key (从表外键字段名)

    references 主表 (主表的主键) ;

    无法直接删除主表已经被从表使用的数据,必须先删除从表的数据.

  • 相关阅读:
    app-授权登录插件配置
    微信公众号-公众号设置-功能设置
    Java变量
    Java数据类型
    计算机存储单元
    Java常量
    k8s
    第一个Java程序
    旋转木马
    tools
  • 原文地址:https://www.cnblogs.com/jia-/p/7082823.html
Copyright © 2011-2022 走看看