标签:
基本查询语句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 |
select * from employee where d_id in (2,3);
注:如果元素是字符,必须加上单引号
select * from employee where age between 15 and 23;
[not] like ‘字符串‘
字符串必须加单引号。
"%"可以用来代表任意长的的字符串,b%k可以代表bk,bok,book
"_"只能表示单个字符。
select * from employee where name like ‘Li%y‘;
select * from employee where name is null;
select * from employee where d_id = 1 and sex like ‘男‘;
select * from employee where age<23 and sex like ‘男‘;
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;
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;
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);
使用该查询语句时,内层查询语句不返回查询记录,而是返回一个真假值,如果查到满足条件的记录则返回true,当返回true时,外层查询语句才进行查询。
mysql> select * from employee where exists (select d_name from department where d_id=1002);
该关键字表示满足其中任意一个条件。只要满足内层查询语句返回的结果中的任何一个,就可以通过该条件来执行外层查询语句。
如下两个表:
mysql> select * from computer_stu where score>= any(select score from schoolship);
如上,可以查询所有可以获得奖学金的学生
该关键字表示满足所有条件。只要满足内层查询语句返回的结果中的所有结果,才可以通过该条件来执行外层查询语句。
将从多个表中查询的数据合并到一起显示,语法如下:
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]‘;
标签:
原文地址:http://blog.csdn.net/wuchao0508/article/details/51582852