您的位置:首页 > 数据库 > MySQL

MySQL查询语句

2015-11-04 21:16 537 查看
*************MySQL查询语句*************

1、单表查询

(1)、查询所有字段

        select 字段1,字段2,字段3,字段..... from 表名;

        select t.*,t.rowid from t_student t;

 (2)、查询指定字段

        select 字段1,字段2,字段3 from 表名;

        select stuName from t_student;

 (3)、where条件查询

        select 字段1,字段2,字段3  from 表名 where 条件表达式;

        select t.* from t_student t where t.age ='18';

 (4)、带in关键字查询

        select 字段1,字段2,字段3 ...from 表名 where 字段 [not] in (元素1,元素2,元素3);

        select t.* from t_student where t.age in ('18','19','20');

 (5)、带between and的范围查询

        select 字段1,字段2,字段3...from 表名 where 字段 [not]  取值1 and 取值2;

         select t.* from t_student where t.age between 12 and 25;

 (6)、带like的模糊查询

        select 字段1,字段2,字段3...from 表名 where 字段 [not] like '字符串';

        “%”-->匹配所有字符

        “_”-->匹配单个字符

 (7)空值查询

 select 字段1,字段2,字段3 ... from 表名 where 字段 is [not]null;

  (8)、带and的多条件查询

  select 字段1,字段2,字段3... from 表名 where 条件表达式1 and 条件表达式2...

  (9)带or的多条件查询

   select 字段1,字段2,字段3... from 表名 where 条件表达式1 or 条件表达式2...

 (10)、distinct 去重复查询

    select distinct 字段名from 表名;

 (11)对查询结果排序

    select 字段1,字段2,字段3...from 表名 order by 属性名[asc|desc]

    (12)、group by分组查询

    group by 属性名 [having 条件表达式]

    <1>单独使用(没什么意义)

    <2>与group_concat()函数一起使用

    select gradName, group_concat(stuName)from t_student group by gradName;

一年级    张三,李四

三年级    许xx,赵XX

二年级    王五,陈xx,李xx

四年级    张凤,宫佳伟

    <3>与聚合函数一起使用

    select stuName ,count(stuName) from t_student group by gradName;

张三    2

许xx    2

王五    3

张凤    2

    <4>与having一起使用(限制输出结果)

select stuName,count(stuName) from t_student group by gradName having count(stuName)<3;

stuName    count(stuName)

张三                2

许xx                2

张凤                2

    <5>与with rollup 一起使用(最后加入总和行)

        select stuName ,count(stuName) from t_student group by gradName with rollup;

张三    2

许xx    2

王五    3

张凤    2

张凤    9             <新加的行>

 

  (13)、分页查询 limit

  select 字段1,字段2,字段3... from 表名 limit 初始位置,记录数;

  select t.* from t_student t limit 0,3;

  select t.* from t_student t limit 3,3;

 

 

 2、聚合函数

 (1)、count()函数

        <1>count ()函数用来统计记录的条数

        select count(*) from t_student;

        <2>与group by 关键字一起使用

        select  count (*) total from t_student group by gradName;

 (2)、sum()函数

        <1>sum ()用来求和

        <2>与group by一起使用

  (3)、avg()函数

        <1>age()用来求平均值

        <2>与group by关键字一起使用

  (4)、max()函数

            <1>max()用来求最大值

            <2>与group by关键字一起使用

  (5)、min()函数

             <1>min()用来求最小值

            <2>与group by关键字一起使用

            

3、连接查询(将两个或两个以上表根据某种关系连接起来,从中选取所需数据)

 (1)、内连接

            内连接查询是最常用的连接查询,内连接查询可以查询两个或两个以上的表

            select t1.bookName,t1.author,t2.bookType from t_book t1,t_bookType t2 where t1.id =t2.id;

  (2)、外链接

        查出某张表的所有信息

        select 属性列表 from 表名 1 left|right join 表名2 on 表1属性=表2属性  注释:on 之后的为条件

      (2.1)左连接

            罗列表1的所有信息而表2只能查出匹配记录(不匹配的null显示)

      (2.2)右连接

            罗列表2所有信息而表1只能查出匹配记录(不匹配的null显示)

 (3)多条件查询

 4、子查询

(1)、带in关键字的子查询

    一个查询语句的条件可能落在另一个select语句的查询结果中

(2)、带比较运算符的子查询

    子查询可使用比较运算符

(3)带exits关键字的子查询

    加入子查询到记录,则进行外层查询,否则不进行外层查询

(4) 带any关键字的子查询

     any关键字表示满足其中任意一个条件

(5)带all关键字的子查询

    all关键字表示满足所有条件

    

5、合并查询

(1)、union

        使用union关键字,会将查询到的所有结果合并到一起,然后去除相同的记录

(2)、union all

        使用union all关键字,是合并所有查询结果,不去除相同记录

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: