• MySQL-查询数据(SELECT)


    MySQL SELECT语句

    SELECT语句用于从表或视图中获取数据

    Select语句组成
    
    Select    之后是逗号分隔列或星号(*)的列表,表示要返回所有列。
    From     指定要查询数据的表或视图。
    Join     根据某些连接条件从其他表中获取数据。
    Where    筛选条件,过滤结果集中的行。
    Group By  将一组行组合成小分组,并对每个小分组应用聚合函数。
    Having   过滤器基于Group By子句定义的小分组。 
    Order By  指定用于排序的列的列表。
    Limit    限制返回行的数量。

    SELECT语句实例应用:


    SELECT lastname, firstname, jobtitle From employees;    #从employees表中取lastname,firstname,jobtitle三列数据
    SELECT * From employees;                     #从employees表中取所有列数据

    对数值进行搜索

    SELECT * from where score>90;                  #显示分数大于95的信息

    对字符串值进行查找

    SELECT first_name From employees where first_name='Tom';  #找出姓Tom的员工

    多类型条件组合查找

    SELECT last_name,first_name,birth,state from employees where birth<'1995-2' and (state='VA' or state='BA');

    NULL值特殊处理各种操作符不能对NULL值进行处理

    NULL值查找采用is null或is not null

    SELECT first_name,birth from employees where death is null

    查询结果排序

    SELECT last_name,first_name from employees order by state DESC,last_name ASC #先按出生地降序排列,同出生地按姓氏升序排列

    http://blog.sina.com.cn/s/blog_5e45d1be0100i0dg.html

    MySQL还可以把表达式的计算结果当作输出列的值

    常用的日期操作:
    MySQL中,年份是处于最前面的。
        1.按日期进行排序

        SELECT * from employees where time between '2015-6-1' and '2016-6-1'

        SELECT * from event where date=’2002-10-01’
        2.查找某个日期或日期范围
          
        SELECT last_name,first_name,birth,state from employees where death>'1990-1-1' and death<'2000-1-1'

        3.提取日期中的年,元,日各个部分三个函数(year,month,dayofmonth可分离出日期中的年月日

        
    SELECT last_name,first_name,birth,state from employees where where month(birth)=7 and dayofmonth(birth) =6;
        4.用一个日期求出另外一个日期(to_days可以把日期转换为天数

        SELECT last_name,first_name,birth to_days(death)-to_days(birth) as age from president
        
        SELECT last_name,first_name,expiration from member where(to_days(expiration)-to_days(curdate())<60

    模式匹配
      
      模糊查询(使用like和not like加上一个带通配符的字符串)
      通配符”_”(单个字符)和”&”(多个字符)

      SELECT concat(first_name,' ',last_name) as name,
      where last_name like 'W%';                #找到W或w开头的人
      where last_name like '%W%';               #找到名字里W或w开头的人
      
    限制查询结果数据行个数

      SELECT last_name,first_name,birth,state from president

      order by birth limit 5;                 #只想看前5个
      
      order by birth limit 10,5;                #返回从第11个记录开始的5个记录(跳过了10个)

      小技巧:从president表中随机找出一个值:

      SELECT last_name,first_name,birth,state from president

      order by rand() limit 1;                #这是用了表达式求值的方法

    生成统计信息
      
      1.查询结果中重复数据清洗(distinct
      
        SELECT distinct state from president      #查询美国总统来自哪个州(不计入重复数据)
      
      2.count()函数统计相关记录的个数
      
        使用方法:count(*)计算所有,NULL也要
             count(数据列名称),NULL值不计算在内
      
        SELECT count(*) from president;

      3.分类统计(count函数结合group by)
        
        方法1:

        SELECT count(*) from student where sex=’f’;
      
        SELECT count(*) from student where sex=’m;

        方法2:    
        
        SELECT sex,count(*) f rom student group by sex;
      
        复杂应用:
        
        查看总统出生最多的前4个州

        select state,count(*) as count from president group by state order by count desc limt4;

    多张表数据提取

        
      数据库可利用“关系”来综合多个数据表里面的记录,这种操作称之为“关联”或“结合”

      SELECT需要给出多个数据表里面的信息(不可重复);

      From需要知道从哪几个表里面做事;

      where则对几个表之间的关联信息作出详细的描述。
      
      1.数据列引用方式:数据表名.数据列名
        
        查询某一天内的学生们的考试成绩,用学号列出
        
        SELECT scroe.student_id,event_date,score.score.event.type
        
        From event,score
        
        where event.date=’2003-09-12’ and event.event_id=score.event_id
        
        首先,利用event数据表把日期映射到一个考试事件编号,再利用这个编号把score表内相匹配的考试分数找出来。关联两个表,完成查询。
        
        查询某一天内的学生们的考试成绩,用姓名列出
        
        SELECT student.name event.name,score.score,event.type
        
        From event,score,student
        
        where event.date=’2003-09-12’ and event.event_id=score.event_id and score.student_id=student.student_id;
        
        查询一下缺席学生的名字,学号,缺席次数

        SELECT student.student_id,student_name

        count(absence.date) as absences

        From student,absence

        where student.student_id=absence.student_id  //关联条件

        group by student.student_id;
  • 相关阅读:
    ASP.NET MVC Model元数据(三)
    ASP.NET MVC Model元数据(二)
    ASP.NET MVC Model元数据(一)
    腾讯云服务器linux centOS7.4 搭建ftp服务器 vsftpd
    腾讯云服务器linux Ubuntu操作系统搭建ftp服务器vsftpd
    linux设置禁止ping
    linux 查询管道过滤,带上标题字段
    安装php xdebug调试工具及性能分析工具webgrind for windows
    linux增加硬盘 磁盘分区格式化及挂载
    virtualBox linux操作系统centos 挂载光盘
  • 原文地址:https://www.cnblogs.com/loser1949/p/8099982.html
Copyright © 2020-2023  润新知