第六章 查询数据

基本查询语句

select的基本语法:

select 属性列表

from 表名和视图列表

[where 条件表达式1]

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

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

 

单表查询

如下表:

   

   

查询所有字段

mysql> select * from employee;

   

   

查询指定字段

select name,sex,homeaddr from employee;

   

   

查询指定记录

where 条件表达式

   

select * from employee where d_id=2;

   

查询条件:

查询条件

关键字

比较

=,<,<=,>,>=,!=,!>,!<

指定范围

between and ,not between and

指定集合

in,not in

匹配字符

like,not like

是否为空

is null,is not null

多个查询条件

and , or

   

带in关键字的查询

select * from employee where d_id in (2,3);

   

注:如果元素是字符,必须加上单引号

   

带between and的范围查询

select * from employee where age between 15 and 23;

   

   

带like的字符匹配查询

[not] like '字符串'

字符串必须加单引号。

"%"可以用来代表任意长的的字符串,b%k可以代表bk,bok,book

"_"只能表示单个字符。

   

select * from employee where name like 'Li%y';

   

   

查询空值

select * from employee where name is null;

   

带and的多条件查询

select * from employee where d_id = 1 and sex like '男';

   

   

select * from employee where age<23 and sex like '男';

   

   

带or的多条件查询,类上

   

查询结果不重复

select distinct d_id from employee;

如果d_id 存在重复,使用该语句,将显示所有该字段的不同值

   

对查询结果排序

order by 属性名 [asc|desc]

   

select * from employee order by age;

   

注:空值默认为最小值

   

分组查询

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

   

a.单独使用group by关键字来分组

select * from employee group by sex;

   

   

如上,按照性别分组后只显示每组的一条记录,因此意义不大。

   

b.group by关键字与group_concat()函数一起使用

select sex,group_concat(name) from employee group by sex;

   

   

c. group by关键字与集合函数一起使用

select sex,count(sex) from employee group by sex;

   

   

d. group by关键字与having一起使用

select sex,count(sex) from employee group by sex having count(sex)>=2;

   

   

e. 多个字段分组

select * from employee group by d_id,sex;

   

f. group by关键字与with rollup一起使用

select sex,count(sex) from employee group by sex with rollup;

   

使用rollup后,新加一条记录来显示分组值得总和

   

mysql> select sex,group_concat(name) from employee group by sex with rollup;

   

   

用limit限制查询结果的数量

select * from employee limit 2;

   

   

可以指定起始位置:

select * from employee limit 1,2;

   

 

指定从第一条开始(默认从第零条开始)

   

   

   

使用集合函数查询

1.count()函数

统计总的记录数

mysql> select count(*) from employee;

   

   

统计不同d_id的记录数

mysql> select d_id,count(*) from employee group by d_id;

   

2.sum()函数

统计如下表某些记录的字段值的总和

   

mysql> select name,sum(grade) from student group by s_id;

   

   

3.avg()函数

类上

4.min()函数

类上

3.max()函数

类上

   

   

   

连接查询

有以下两张表:

   

   

   

   

内连接查询

mysql> select num,name,employee.d_id,sex,d_name,function

-> from employee,department

-> where employee.d_id = department.d_id;

   

   

外连接

select 属性名列表

from 表名1 left| right join 表名2

on 表名1.属性名1 = 表名2.属性名2;

   

"属性名列表"参数表示要查询的字段,他们可以来自不同表。

"表名1"和"表名2"参数表示将这两个表进行外连接。

"left"和"right"指定左连接查询还是右连接查询。

"on"后面是连接条件

   

a.左连接查询

mysql> select num,name,employee.d_id,age,sex,d_name,function from employee left join department on employee.d_id=department.d_id;

   

先取出employee表数据,再根据连接条件取出department表的数据

b.右连接查询

mysql> select num,name,employee.d_id,age,sex,d_name,function from employee right join department on employee.d_id=department.d_id;

   

先取出department表的数据,再根据连接条件取出employee表数据

   

复合条件连接查询

mysql> select num,name,employee.d_id,sex,d_name,function from employee,department where employee.d_id = department.d_id and age<24;

   

   

   

   

   

子查询

带in关键字的子查询

mysql> select * from employee where d_id in (select d_id from department);

   

   

mysql> select * from employee where d_id not in (select d_id from department);

   

   

带比较运算符的子查询

有以下两个表:

   

   

mysql> select id,name,score from computer_stu

-> where score>=(select score from schoolship where level=1);

   

   

   

带exists关键字的子查询

使用该查询语句时,内层查询语句不返回查询记录,而是返回一个真假值,如果查到满足条件的记录则返回true,当返回true时,外层查询语句才进行查询。

   

mysql> select * from employee where exists (select d_name from department where d_id=1002);

   

   

带any关键字的子查询

该关键字表示满足其中任意一个条件。只要满足内层查询语句返回的结果中的任何一个,就可以通过该条件来执行外层查询语句。

   

如下两个表:

   

   

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

   

如上,可以查询所有可以获得奖学金的学生

   

带all关键字的子查询

该关键字表示满足所有条件。只要满足内层查询语句返回的结果中的所有结果,才可以通过该条件来执行外层查询语句。

   

合并查询结果

将从多个表中查询的数据合并到一起显示,语法如下:

select 语句1

union|union all

select 语句2

union|union all

select 语句3;

   

union关键字表示将查询结果合并后,除掉相同记录。union all则是简单的合并。

   

mysql> select d_id from employee

-> union

-> select d_id from department;

   

mysql> select d_id from employee

union all

select d_id from department;

   

   

   

   

为表和字段取别名

为表取别名

mysql> select * from department dpt where dpt.d_id =1001;

   

   

   

为字段取别名

mysql> select name as employee_name,age as employee_age from employee;

   

   

   

使用正则表达式查询

基本形式:

属性名 regexp '匹配方式'

   

模式字符

含义

^

匹配字符串开始的地方

$

匹配字符串结束的地方

.

代表字符串中的任意一个字符,包括回车和换行

[字符集合]

匹配字符集合中的任意字符

[^字符集合]

匹配字符集合以外的任意字符

S1|S2|S3

匹配S1|S2|S3中的任意一个字符串

*

代表多个该字符串之前字符,包括0个和1个

+

代表多个字符串之前的字符,包括1个

字符串{N}

字符串出现N次

字符串{M,N}

字符串出现至少M次,至多N次

   

查询特性字符串开头的记录

mysql> select * from employee where name regexp '^K';

   

   

查询特性字符串结尾的记录

mysql> select * from employee where name regexp 'y$';

   

   

用符号"."来代替字符串的任意一个字符

mysql>select * from employee where name regexp '^K...y';

   

   

匹配指定字符中的任意一个

mysql> select * from employee where name regexp '[ks]';

   

   

匹配指定字符串以外的字符

mysql> select * from employee where name regexp '[^a-zA-Z]';

   

  

原文地址:https://www.cnblogs.com/wuchaodzxx/p/5524952.html