zoukankan      html  css  js  c++  java
  • MySQL笔记---单表查询1

    MySQL笔记---单表查询1

    /**
      单表查询
     */
    show databases;
    use db_26;
    show tables;
    
    select *
    from tab_3;
    desc tab_3;
    insert into tab_3 (tid, tname, tage, sex)
    values (1001,
            concat('韩寒', 1),
            truncate((rand() * 8 + 18), 0),
            (if(rand() > 0.5, '男', '女')));
    
    
    /**
      单表查询
     */
    select *
    from tab_3;
    # 查询所有行的所有列, select * from 表名
    select tid, tname
    from tab_3;
    # 查询所有行的指定列 select 列名,列名 from 表名
    select tage + 1, concat('我叫', tname, ' 今年', tage, ' 岁')
    from tab_3;
    # 列运算 + - * / %(mod)
    # concat(str,str2,str3,.....) 字符串拼接运算
    
    select distinct tname
    from tab_3;
    select distinct tname, tage
    from tab_3;
    # 去重 distinct 重复的记录显示一次
    
    # 聚合函数: sum max min avg count
    select sum(tage), min(tage), max(tage), avg(tage), count(tage), count(*)
    from tab_3;
    # count 不为 null 的数量 ,count(*) 总记录数
    # avg ,计算非 null 的值
    
    insert into tab_3 (tid, tname)
    values (23, '张三');
    # 不设置,默认为 null
    
    # 求所有记录的平均年龄
    select sum(tage) / count(*), avg(tage)
    from tab_3;
    # 17.3571       20.2500
    
    /**
      起别名
      as 可以省略
     */
    select sum(tage)   as '年龄总和',
           min(tage)   as '最小值',
           max(tage)   as '最大值',
           avg(tage)   as '非 null 的平均值',
           count(tage) as '非 null 的总人数',
           count(*)    as '全部的总人数'
    from tab_3;
    # 给 字段 起别名
    
    select sum(t.tage)   as '年龄总和',
           min(t.tage)   as '最小值',
           max(t.tage)   as '最大值',
           avg(t.tage)   as '非 null 的平均值',
           count(t.tage) as '非 null 的总人数',
           count(*)      as '全部的总人数'
    from tab_3 as t;
    # 给 表 起别名
    
    /**
      like 模糊查询
     */
    select *
    from tab_3
    where tname like '%1%';
    # 查询名字中含有 1 的人,  % 表示 0-多个字符
    
    select *
    from tab_3
    where tname like '__1%';
    # 查询名字中第三个字符是1的人  _ 表示1个字符
    
    
    /**
      分页查询 limit n,m
      n为索引(从0开始),m 是获取的记录数量
      ⚠️ limit 是 MySQL的方言
     */
    select *
    from tab_3;
    
    select *
    from tab_3
    limit 0,4;
    # 查询前四行
    select *
    from tab_3
    limit 4,4;
    # 从第 5 行开始,查询 4 行
    
    
    /**
      条件查询 where 条件
      运算符 > >= < <= != = <>
      条件: or , and
      判断是否是null: is null , is not null
      范围: x between a and b  [a,b]                  x in (x1,x2,x3)
    
      ⚠️:不能在 where 后加 聚合函数
     */
    select *
    from tab_3
    where tage != 9
       or tage is null;
    # 年龄不等于 9 或者 等于 null
    select *
    from tab_3
    where tage > 20
      and sex = '女';
    # 年龄大于20,并且 性别为 女
    select *
    from tab_3
    where tage between 20 and 25;
    # 查找 age 在 20 和 25 之间的人 [20,25]
    select *
    from tab_3
    where tage in (20, 22, 24);
    # 查找 年龄 = 20 或者 22 或者 24 的人
    
    select *
    from tab_3
    where tage > any_value(20);
    # 没用的东西
    
    /**
      null 转换
      ifnull(field,value) 如果 filed 为 null 时,按 value 运算
     */
    select tage '年龄', tage + 1 '明年的年龄'
    from tab_3;
    select concat('我叫', tname, '今年', tage, '岁')
    from tab_3;
    # 任何数据和 null 运算,结果还是 null
    
    select tage '年龄', ifnull(tage, 0) + 1
    from tab_3;
    select concat('我叫', ifnull(tname, '无名'), '今年', ifnull(tage, 0), '岁')
    from tab_3;
    
    select database();
    # 获取当前数据库
    
  • 相关阅读:
    包含源文件 —— 是奇技淫巧还是饮鸩止渴?
    感谢各位网友——《品悟C——抛弃 C程序设计 中的谬误与恶习》正式出版
    能否用痰盂盛饭——谈谈在头文件中定义外部变量
    C:劣书简易鉴别法
    从“站在巨人的肩上”到“跪到侏儒之脚下”——图灵公司副主编自供(二)
    劣质代码评析——刻舟求剑的故事
    Python自然语言处理学习笔记(36): 4.8 Python库的样本
    Python自然语言处理学习笔记(38): 4.10 深入阅读
    Python自然语言处理学习笔记(37):4.9 小结
    Python自然语言处理学习笔记(35): 4.7 算法设计
  • 原文地址:https://www.cnblogs.com/javayanglei/p/13305271.html
Copyright © 2011-2022 走看看