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;

MySQL-查询数据(SELECT)的更多相关文章

  1. MySQL行(记录)的详细操作一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理

    MySQL行(记录)的详细操作 阅读目录 一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理 一 介绍 MySQL数据操作: ...

  2. mysql 查询数据时按照A-Z顺序排序返回结果集

    mysql 查询数据时按照A-Z顺序排序返回结果集 $sql = "SELECT * , ELT( INTERVAL( CONV( HEX( left( name, 1 ) ) , 16, ...

  3. MySQL 查询数据

    MySQL 查询数据 MySQL 数据库使用SQL SELECT语句来查询数据. 你可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据. 语法 以下为在MyS ...

  4. MySQL查询数据表中数据记录(包括多表查询)

    MySQL查询数据表中数据记录(包括多表查询) 在MySQL中创建数据库的目的是为了使用其中的数据. 使用select查询语句可以从数据库中把数据查询出来. select语句的语法格式如下: sele ...

  5. 十二、MySQL 查询数据

    MySQL 查询数据 MySQL 数据库使用SQL SELECT语句来查询数据. 你可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据. 语法 以下为在MyS ...

  6. navicat for Mysql查询数据不能直接修改

    navicat for Mysql查询数据不能直接修改 原来的sql语句: <pre> select id,name,title from table where id = 5;</ ...

  7. 使用Python3导出MySQL查询数据

    整理个Python3导出MySQL查询数据d的脚本. Python依赖包: pymysql xlwt Python脚本: #!/usr/bin/env python # -*- coding: utf ...

  8. php MySQL 查询数据

    以下为在MySQL数据库中查询数据通用的 SELECT 语法: SELECT column_name,column_name FROM table_name [WHERE Clause] [LIMIT ...

  9. 吴裕雄--天生自然MySQL学习笔记:MySQL 查询数据

    MySQL 数据库使用SQL SELECT语句来查询数据. 可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据. 语法 以下为在MySQL数据库中查询数据通用 ...

  10. 使用Connector / Python连接MySQL/查询数据

    使用Connector / Python连接MySQL connect()构造函数创建到MySQL服务器的连接并返回一个 MySQLConnection对象 在python中有以下几种方法可以连接到M ...

随机推荐

  1. i++为什么不能作为左值,而++i可以作为左值

    今天看书见到如下代码: int a=2; ++a++; 根据操作符的优先级和结合性知,操作符++的优先级为3,结合性为右结合,即++a++;可以理解为++(a++); 但我把代码放在vs2015上,结 ...

  2. Go:冒泡排序

    package main import "fmt" func BubbleSort(arr *[5]int) { fmt.Println("排序前:", *ar ...

  3. leetcode-832翻转图像

    翻转图像 思路: 先对图像进行水平翻转,然后反转图片(对每个像素进行异或操作) 代码: class Solution: def flipAndInvertImage(self, A: List[Lis ...

  4. selenium实战演练

    利用selenium以及pyquery,爬取当当网图书信息,并且将数据存入文件以及MongoDB数据库中. 配置文件: key="python" MONGO_URL='localh ...

  5. PHP 真值与空值

    本文参考 http://php.net/manual/en/types.comparisons.php. 1. isset bool isset ( mixed $var [, mixed $... ...

  6. UVA 213 信息解码(二进制&位运算)

    题意: 出自刘汝佳算法竞赛入门经典第四章. 考虑下面的01串序列: 0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1 ...

  7. Springboot开启事务

    参考资料: https://blog.csdn.net/message_lx/article/details/77584847

  8. HTML基础知识 table中 th,td,tr

    https://www.2cto.com/kf/201711/701872.html table是一个布局神器,之前看过很多代码,都是用table布局的.但是,我在学习的过程中,发现table有很迷的 ...

  9. hdu_1028_Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  10. Delphi ADO的Lookup类型字段的问题

    关于ADO数据集控件中的Lookup类型字段,在其Lookupkeyfields属性指向的字段中存在NULL值的,就会出现'EOleException with message '发生未知错误',这个 ...