MySQL基本操作-SQL查询语句

1.查询数据

   select  属性列表

            from  表名和视图列表

            [where  条件表达式1]

            [group by 属性名1  [having 条件表达式2] [with rollup最后加一条记录是上面记录的总和]]  //按照属性名1指定的字段分组,有having则要满足条件表达式2

     [order  by 属性名2  [asc|desc]]

      where查询条件:

      比较          =、<、<=、 >、>=、!=、<>、!>、!<

      指定范围     between and、not between and

      指定集合     in、not in

      匹配字符     like、not like <%:代表任意长度字符串,_:代表单个字符>

      是否为空值  is null、is not null

eg:select * from test0 where id in (1001,1004,1005);

     select * from test0 where id between 1002 and 1005;

   select * from test0 where id < 1004 and name like 'h_h_'; //两个条件均满足

     select * from test0 where id < 1004 or name like 'h%'; // 满足其中一个便可

   select distinct name from test0;   //查询结果不重复 

   select id,group_concat(name) from test0 group by name;//将name分组中指定字段值都显示出来

   select name,count(name) from test0 group by name;//将name分组的每一组记录数统计出来

   select name,count(name) from test0 group by name having count(name) > 1; //显示记录数大于1的

     having表达式是作用于分组后的记录,而where作用于表或者视图。

   select name,count(name) from test0 group by name with rollup;

     select * from test0 limit 3; //只显示3条记录

     select * from test0 limit 1,3; //从第2条记录起显示3条记录

  使用集合函数查询

      count()用来统计记录的条数,sum()用来计算字段的值的总和,avg()用来计算字段的值的平均值,max()用来查询字段的最大值,min()用来查询字段的最小值。

        select count(*) from test0; //统计test0表的记录数

        select count(*) from test0 group by name; //先将name字段分组,然后再计算每个分组的记录数

        select sum(score) from grade group by num;//先按num分组,然后统计各个组的成绩总和

        select avg(score) from grade group by course; //先按科目分组,然后计算出每组的平均成绩

        select max(score) from grade group by course; //先按科目分组,然后计算每组的最大值

2.连接查询

  1).内连接查询:只有表中有值相同的字段才能做连接查询

     select id,name,d_id,d_name from test0,test1 where test0.id  = test1.d_id; 

  2).外连接查询

     select 属性名列表  from 表名1 left|right join 表名2 on 表名1.属性名1 = 表名2.属性名2;

     left左连接查询:可以查询出"表名1"所指的表中的所有记录,而表名2所指的表中,只能查询出匹配的记录。

     right右连接查询:与left相反。

 

3.子查询:将一个查询语句嵌套在另一个查询语句中,内层查询语句的查询结果可以作为外层查询语句的查询条件。

   (1).select * from employee where d_id  in (select d_id from department);

   (2).带比较运算的子查询: =、 !=、>、>=、<、<=、<>等

        select id,name from test0 where score >= (select score from scholarship where level=1 );

   (3).exists关键字表示存在。 内层查询语句查询到满足条件的记录就返回一个真值(true),否则返回一个假值(false),当返回值为true时外层查询语句将进行查询否则不进行查询。

       select * from employee where exists (select d_name from department where d_id=1003);//如果department表中存在d_id为1003则执行select * from employee . not exists的结果相反

   (4).any关键字表示满足其中任一条件。

     select * from computer_stu where score >=any (select score from scholarship);

   (5).all关键字: 只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。

        select * from computer_stu where score>=all (select score from scholarship);//只有computer_stu中的分数高于(all)所有scholarship的结果才会被查询出来。

   (6).合并查询结果

        select 语句1 union| union all select 语句2...

       union:合并查询结果时,需要将相同的记录消除掉

       union all:不会消除掉相同的记录,会将所有的记录合并到一起。

4.为表取别名:

   select * from department d where d.d_id = 1001; //将department表取了一个别名 d

   为字段取别名:

   select  id as d_id,name as d_name  from test0;

posted @ 2015-10-13 12:07  MingsHsu  阅读(172)  评论(0编辑  收藏  举报