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. java线程池,信号量使用demo

    直接上代码 package org.jimmy.threadtest20181121; import java.util.concurrent.LinkedBlockingQueue; import ...

  2. 将Jar安装到本地仓库和Jar上传到私服

    举例 1. 依赖如下: <dependency> <groupId>org.quartz-scheduler.internal</groupId> <arti ...

  3. router-link的使用方法

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. DB2隔离级别

    四.隔离级别与锁 数据库是利用锁和隔离级别来共同处理数据库的并发的.DB2数据库用来尝试实施并发性的方法之一是通过使用隔离级别,它决定在第一个事务访问数据时,如何对其他事务锁定或隔离该事务所使用的数据 ...

  5. 一个简单的模板了解css+div网页布局

    直接附上最终效果图: index.html内容: <html> <!--20170730 soulsjie--> <head> <meta http-equi ...

  6. HDU-3790最短路径问题,第十遍终于过了~

    最短路径问题                                                                   Time Limit: 2000/1000 MS (J ...

  7. [luoguP1056] 排座椅(sort + 模拟)

    传送门 nc题,一直sort就过了 代码 #include <cstdio> #include <iostream> #include <algorithm> #d ...

  8. Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

    C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell ...

  9. poj 3648 2-sat 输出任意一组解模板

    转载地址:http://blog.csdn.net/qq172108805/article/details/7603351 /* 2-sat问题,题意:有对情侣结婚,请来n-1对夫妇,算上他们自己共n ...

  10. P1340 兽径管理 洛谷

    https://www.luogu.org/problem/show?pid=1340 题目描述 约翰农场的牛群希望能够在 N 个(1<=N<=200) 草地之间任意移动.草地的编号由 1 ...