零基础学习MySql数据库—3

一、select 查询

1.1 select 的简单使用

语法:

select [distinct] *| {column1,column2,...} from tbl_name;

创建一张学生表来测试:

create table student (
id int not null default 1,
name varchar(20) not null default '',
chinese float not null default 0.0 comment '语文成绩',
english float not null default 0.0 comment '英语成绩',
math float not null default 0.0 comment '数学成绩'
);

插入数据:

insert into student values(1, '李涛', 89,78, 90);
insert into student values(2, '唐僧', 67,98, 56);
insert into student values(3, '孙悟空', 87,78, 77);
insert into student values(4, '老妖婆', 88,98, 90);
insert into student values(5, '红孩儿', 82,84, 67);
insert into student values(6, '如来佛祖', 55,85, 45);
insert into student values(7, '菩萨', 75,65, 30);

可以指定查询哪些列,比如:查询id,姓名,数学成绩

mysql> select id, name,math from student;
+----+----------+------+
| id | name     | math |
+----+----------+------+
|  1 | 李涛     |   90 |
|  2 | 唐僧     |   56 |
|  3 | 孙悟空   |   77 |
|  4 | 老妖婆   |   90 |
|  5 | 红孩儿   |   67 |
|  6 | 如来佛祖 |   45 |
|  7 | 菩萨     |   30 |
+----+----------+------+
7 rows in set (0.00 sec)
  • 号表示查询所有列(星号效率很低,用哪些字段就取哪些字段):
mysql> select * from student;
+----+----------+---------+---------+------+
| id | name     | chinese | english | math |
+----+----------+---------+---------+------+
|  1 | 李涛     |      89 |      78 |   90 |
|  2 | 唐僧     |      67 |      98 |   56 |
|  3 | 孙悟空   |      87 |      78 |   77 |
|  4 | 老妖婆   |      88 |      98 |   90 |
|  5 | 红孩儿   |      82 |      84 |   67 |
|  6 | 如来佛祖 |      55 |      85 |   45 |
|  7 | 菩萨     |      75 |      65 |   30 |
+----+----------+---------+---------+------+
7 rows in set (0.00 sec)

distinct 如果结果中有完全相同的行,就去除重复行

mysql> select distinct math from student;
+------+
| math |
+------+
|   90 |
|   56 |
|   77 |
|   67 |
|   45 |
|   30 |
+------+
6 rows in set (0.00 sec)
  • 在select语句中可以使用表达式对查询的列进行运算
  • select语句中可以使用as起别名

语法:

select column as 别名from 表;

练习:
1.在所有学生分数上加上10分(查询所有学生的总分再加10分):

mysql> select name, chinese+english+math+10 as total from student;
+----------+-------+
| name     | total |
+----------+-------+
| 李涛     |   267 |
| 唐僧     |   231 |
| 孙悟空   |   252 |
| 老妖婆   |   286 |
| 红孩儿   |   243 |
| 如来佛祖 |   195 |
| 菩萨     |   180 |
+----------+-------+
7 rows in set (0.00 sec)

2.将所有姓唐的学生成绩增加60%(查询总成绩增加60%):

mysql> select name, (chinese+english+math)*1.6 as total from student where name like '唐%';
+------+-------+
| name | total |
+------+-------+
| 唐僧 | 353.6 |
+------+-------+
1 row in set (0.00 sec)

1.2 select 的 where 子句

使用 where 子句,进行查询过过滤。
在 where 子句中经常使用的运算符:

比较运算符:

>、<、<=、>=、=、<>、!= 大于、小于、小于等于、大于等于、等于、不等于、不等于
between … and … 显示在某一区间的值
in() 显示在 in 列表中的值,如:in(100, 200)
like ” 模糊查询
not like ’ ‘ 模糊查询
is null 判断是否为空

逻辑运算符:

and 多个条件同时成立
or 多个条件任一成立
not 不成立,如:where not(math > 90)

示例:查询姓李的学生的成绩

mysql> select * from student where name like '李%';
+----+------+---------+---------+------+
| id | name | chinese | english | math |
+----+------+---------+---------+------+
|  1 | 李涛 |      89 |      78 |   90 |
+----+------+---------+---------+------+
1 row in set (0.00 sec)

查询英语成绩大于90分的同学:

mysql> select * from student where english > 90;
+----+--------+---------+---------+------+
| id | name   | chinese | english | math |
+----+--------+---------+---------+------+
|  2 | 唐僧   |      67 |      98 |   56 |
|  4 | 老妖婆 |      88 |      98 |   90 |
+----+--------+---------+---------+------+
2 rows in set (0.00 sec)

查询总分大于200分的同学:

mysql> select id, name, math+english+chinese as 'total' 
                from student where math+english+chinese>200;
+----+--------+-------+
| id | name   | total |
+----+--------+-------+
|  1 | 李涛   |   257 |
|  2 | 唐僧   |   221 |
|  3 | 孙悟空 |   242 |
|  4 | 老妖婆 |   276 |
|  5 | 红孩儿 |   233 |
+----+--------+-------+
5 rows in set (0.00 sec)

查询姓李并且 id 大于 10 的学生:

mysql> select * from student where name like '李%' and id > 10;
Empty set (0.00 sec)

查询英语成绩大于语文成绩的学生:

mysql> select * from student where english > chinese;
+----+----------+---------+---------+------+
| id | name     | chinese | english | math |
+----+----------+---------+---------+------+
|  2 | 唐僧     |      67 |      98 |   56 |
|  4 | 老妖婆   |      88 |      98 |   90 |
|  5 | 红孩儿   |      82 |      84 |   67 |
|  6 | 如来佛祖 |      55 |      85 |   45 |
+----+----------+---------+---------+------+
4 rows in set (0.00 sec)

查询总分大于200分并且数学成绩小于语文成绩的姓唐的学生:

mysql> select * from student where (math+english+chinese) > 200 
                            and math < chinese and name like '唐%';
+----+------+---------+---------+------+
| id | name | chinese | english | math |
+----+------+---------+---------+------+
|  2 | 唐僧 |      67 |      98 |   56 |
+----+------+---------+---------+------+
1 row in set (0.00 sec)

查询英语成绩在 80 ~ 90 之间的学生:

以下两种方法等价:
mysql> select * from student where english between 80 and 90;   -- between...and...方法是闭区间

mysql> select * from student where english>=80 and english<=90;

+----+----------+---------+---------+------+
| id | name     | chinese | english | math |
+----+----------+---------+---------+------+
|  5 | 红孩儿   |      82 |      84 |   67 |
|  6 | 如来佛祖 |      55 |      85 |   45 |
+----+----------+---------+---------+------+
2 rows in set (0.00 sec)

查询数学成绩为 89,90,91 的学生:

两种方法:
mysql> select * from student where math=89 or math=90 or math=91; 

mysql> select * from student where math in(89,90,91); 

+----+--------+---------+---------+------+
| id | name   | chinese | english | math |
+----+--------+---------+---------+------+
|  1 | 李涛   |      89 |      78 |   90 |
|  4 | 老妖婆 |      88 |      98 |   90 |
+----+--------+---------+---------+------+
2 rows in set (0.00 sec)

1.3 select 的 order by 子句

order by 子句排序查询结果,语法:

select column1,column2,... from table order by column asc|desc,...;
  • order by 指定排序的列,排序的列可以使表中的列名,也可以是select语句后指定的别名
  • asc升序(默认),desc降序
  • order by 子句应该位于select语句的结尾

示例:
对数学成绩进行排序:

mysql> select * from student order by math;
+----+----------+---------+---------+------+
| id | name     | chinese | english | math |
+----+----------+---------+---------+------+
|  7 | 菩萨     |      75 |      65 |   30 |
|  6 | 如来佛祖 |      55 |      85 |   45 |
|  2 | 唐僧     |      67 |      98 |   56 |
|  5 | 红孩儿   |      82 |      84 |   67 |
|  3 | 孙悟空   |      87 |      78 |   77 |
|  1 | 李涛     |      89 |      78 |   90 |
|  4 | 老妖婆   |      88 |      98 |   90 |
+----+----------+---------+---------+------+
7 rows in set (0.00 sec)

由以上结果可知,没有指定升序还是降序,默认是升序。

对总分进行排序后,按从高到低排序输出:

mysql> select id, name, math+english+chinese as total from student order by total desc;
+----+----------+-------+
| id | name     | total |
+----+----------+-------+
|  4 | 老妖婆   |   276 |
|  1 | 李涛     |   257 |
|  3 | 孙悟空   |   242 |
|  5 | 红孩儿   |   233 |
|  2 | 唐僧     |   221 |
|  6 | 如来佛祖 |   185 |
|  7 | 菩萨     |   170 |
+----+----------+-------+
7 rows in set (0.00 sec)

对行李的学生按成绩进行从低到高排序,(因为表中只有一个姓李的学生,所以我插入一条记录):

mysql> insert into student values(8,'李雅',79,31,56);

mysql> select id, name, math+english+chinese as total from student where name like '李%' order by total;
+----+------+-------+
| id | name | total |
+----+------+-------+
|  8 | 李雅 |   166 |
|  1 | 李涛 |   257 |
+----+------+-------+
2 rows in set (0.00 sec)

1.4 count

count(列名) 返回某一列,行的总数,语法:

select count(*)|count(列名) from tbl_name where condition;

示例:
统计一个班有多少名学生:

mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
|        8 |
+----------+
1 row in set (0.00 sec)

统计数学成绩大于等于90的学生有多少人:

mysql> select count(*) from student where math>=90;
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

统计总分大于250的人数有多少:

mysql> select count(*) from student where math+english+chinese>250;
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

说明:count(*)会统计一共的记录数,count(列名)会排除为null的情况

1.5 sum

sum 函数返回满足 where 条件的行的和。
语法:

select sum(列名){,sum(列名)...} from tbl_name [where condition]

示例:统计一个班级数学总成绩:

mysql> select sum(math) from student;
+-----------+
| sum(math) |
+-----------+
|       511 |
+-----------+
1 row in set (0.01 sec)

统计一个班级语文,英语,数学各科的总成绩:

mysql> select sum(chinese),sum(math),sum(english) from student;
+--------------+-----------+--------------+
| sum(chinese) | sum(math) | sum(english) |
+--------------+-----------+--------------+
|          622 |       511 |          617 |
+--------------+-----------+--------------+
1 row in set (0.00 sec)

统计一个班总成绩之和:

mysql> select sum(chinese+english+math) from student;
+---------------------------+
| sum(chinese+english+math) |
+---------------------------+
|                      1750 |
+---------------------------+
1 row in set (0.00 sec)

统计一个班语文成绩平均分:

mysql> select sum(chinese)/count(chinese) from student;
+-----------------------------+
| sum(chinese)/count(chinese) |
+-----------------------------+
|                       77.75 |
+-----------------------------+
1 row in set (0.00 sec)

注意:sum仅对数值起作用,否则结果无意义。

1.6 avg

avg函数返回满足 where 条件的一列的平均值:

select avg(列名) [,avg(列名),...] from tbl_name [where condition];

示例:求一个班级的数学平均分

mysql> select avg(math) from student;
+-----------+
| avg(math) |
+-----------+
|    63.875 |
+-----------+
1 row in set (0.00 sec)

求一个班级总分的平均分:

mysql> select avg(chinese+english+math) from student;
+---------------------------+
| avg(chinese+english+math) |
+---------------------------+
|                    218.75 |
+---------------------------+
1 row in set (0.00 sec)

1.7 max/min

max/min函数返回满足where条件的一列的最大/最小值。

select max(列名) from tbl_name [where condition]

示例:求班级中总分的最高分与最低分

mysql> select max(chinese+math+english),min(chinese+math+english) from student;
+---------------------------+---------------------------+
| max(chinese+math+english) | min(chinese+math+english) |
+---------------------------+---------------------------+
|                       276 |                       166 |
+---------------------------+---------------------------+
1 row in set (0.00 sec)

1.8 group by 子句的使用

group by 子句对列进行分组。

select column1, column2, .. from table group by column;

为了讲清楚分组,创建一个雇员信息表 密码:73k8(来自oracle 9i的经典测试表)
1. EMP员工表
2. DEPT部门表
3. SALGRADE工资等级表

示例:显示每个部门的平均工资和最高工资

mysql> select deptno,avg(sal),max(sal) from EMP group by deptno;
+--------+-------------+----------+
| deptno | avg(sal)    | max(sal) |
+--------+-------------+----------+
|     10 | 2916.666667 |  5000.00 |
|     20 | 2175.000000 |  3000.00 |
|     30 | 1566.666667 |  2850.00 |
+--------+-------------+----------+
3 rows in set (0.01 sec)

显示每个部门的每种岗位的平均工资和最低工资:

mysql> select avg(sal),min(sal),job,deptno from EMP group by deptno,job;
+-------------+----------+-----------+--------+
| avg(sal)    | min(sal) | job       | deptno |
+-------------+----------+-----------+--------+
| 1300.000000 |  1300.00 | CLERK     |     10 |
| 2450.000000 |  2450.00 | MANAGER   |     10 |
| 5000.000000 |  5000.00 | PRESIDENT |     10 |
| 3000.000000 |  3000.00 | ANALYST   |     20 |
|  950.000000 |   800.00 | CLERK     |     20 |
| 2975.000000 |  2975.00 | MANAGER   |     20 |
|  950.000000 |   950.00 | CLERK     |     30 |
| 2850.000000 |  2850.00 | MANAGER   |     30 |
| 1400.000000 |  1250.00 | SALESMAN  |     30 |
+-------------+----------+-----------+--------+
9 rows in set (0.00 sec)

说明:首先按照deptno分组,然后各组再按照job进行分组。

二、函数

2.1 常用日期函数

current_date() 当前日期
current_time() 当前时间
current_timestamp() 当前时间戳
date(datetime) 返回 datetime 的日期部分
date_add(date2, interval d_value d_type) 在 date2 中加上日期或时间
date_sub(date2, interval d_value d_type) 在 date2 上减去一个时间
datediff(date1, date2) 两个日期差(结果是相差的天数)
now() 当前时间

获取当前的年月日:

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2018-08-10     |
+----------------+
1 row in set (0.00 sec)

获取当前时间(时分秒):

mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 16:33:51       |
+----------------+
1 row in set (0.00 sec)

获取当前时间戳:

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2018-08-10 16:34:25 |
+---------------------+
1 row in set (0.00 sec)

在日期的基础上加日期:

mysql> select date_add('2018-08-10', interval 36 day);
+-----------------------------------------+
| date_add('2018-08-10', interval 36 day) |
+-----------------------------------------+
| 2018-09-15                              |
+-----------------------------------------+
1 row in set (0.00 sec)

在日期的基础上减去时间:

mysql> select date_sub('2018-08-10', interval 36 day);
+-----------------------------------------+
| date_sub('2018-08-10', interval 36 day) |
+-----------------------------------------+
| 2018-07-05                              |
+-----------------------------------------+
1 row in set (0.00 sec)

计算两个日期之间相差多少天:

mysql> select datediff(now(), '2018-09-01');
+-------------------------------+
| datediff(now(), '2018-09-01') |
+-------------------------------+
|                           -22 |
+-------------------------------+
1 row in set (0.00 sec)

示例1:创建一张表,记录生日:

mysql> create table t_birthday (
    -> id int primary key auto_increment,
    -> birthday date
    -> );

添加当前日期:

mysql> insert into t_birthday(birthday) values(current_date());
Query OK, 1 row affected (0.01 sec)

mysql> select * from t_birthday;
+----+------------+
| id | birthday   |
+----+------------+
|  1 | 2018-08-10 |
+----+------------+
1 row in set (0.00 sec)

示例2:创建一个留言表

mysql> create table msg (
    -> id int primary key auto_increment,
    -> content varchar(30) not null,
    -> sendtime datetime
    -> );
Query OK, 0 rows affected (0.06 sec)

插入数据:

mysql> insert into msg(content,sendtime) values('hello1', now()); 
Query OK, 1 row affected (0.01 sec)

mysql> insert into msg(content,sendtime) values('hello2', now());
Query OK, 1 row affected (0.01 sec)

mysql> select * from msg;
+----+---------+---------------------+
| id | content | sendtime            |
+----+---------+---------------------+
|  1 | hello1  | 2018-08-10 16:43:55 |
|  2 | hello2  | 2018-08-10 16:44:09 |
+----+---------+---------------------+
2 rows in set (0.00 sec)

显示所有留言信息,发布日期只显示日期,不用显示时间:

mysql> select content,date(sendtime) from msg;
+---------+----------------+
| content | date(sendtime) |
+---------+----------------+
| hello1  | 2018-08-10     |
| hello2  | 2018-08-10     |
+---------+----------------+
2 rows in set (0.00 sec)

查询在十分钟内发布的留言:

mysql> select * from msg where date_add(sendtime, interval 10 minute) > now();
+----+---------+---------------------+
| id | content | sendtime            |
+----+---------+---------------------+
|  1 | hello1  | 2018-08-10 16:43:55 |
|  2 | hello2  | 2018-08-10 16:44:09 |
+----+---------+---------------------+
2 rows in set (0.00 sec)

日期函数使用细节:
date_ add()和date_ sub()中的interval后面可以使year minute second day。
datediff(date1, date2)得到的是天数,二期是date1-date2的天数,因此可以使负数。

2.2 字符串函数

charset(str) 返回字符串字符集
concat(string [, …]) 连接字符串
instr(string, substring) 返回 substring 在 string 中出现的位置,没有就返回0
ucase(string) 转换成大写
lcase(string) 转换成小写
left(string, length) 从 string 的左边起取 length 个字符
length(string) 返回 string 的长度(字节数)
replace(str, search_str, replace_str) 在 str 中用 replace_str 替换 search_str
strcmp(string1, string2) 逐字符比较两字符的大小
substring(str, position [, length]) 从 str 的下标为 position 开始,取 length 个字符, 如果没有 length 就取到 str 末尾
ltrim(string) rtrim(string) trim(string) 去除前空格或者后空格

示例:
获取 emp 表中的 ename 列的字符集:

mysql> select charset(ename) from EMP;
+----------------+
| charset(ename) |
+----------------+
| utf8           |
+----------------+

显示 student 表中的信息,显示格式:“XXX的语文成绩是XXX分,数学XXX分,英语XXX分”:

mysql> select concat(name,'的语文是',chinese,'分, 数学是', math, '分, 英语是', english, '分')
    ->  as '分数' from student;
+----------------------------------------------+
| 分数                                         |
+----------------------------------------------+
| 李涛的语文是89分, 数学是90分, 英语是78分        |
| 唐僧的语文是67分, 数学是56分, 英语是98分        |
| 孙悟空的语文是87分, 数学是77分, 英语是78分      |
| 老妖婆的语文是88分, 数学是90分, 英语是98分      |
| 红孩儿的语文是82分, 数学是67分, 英语是84分      |
| 如来佛祖的语文是55分, 数学是45分, 英语是85分    |
| 菩萨的语文是75分, 数学是30分, 英语是65分        |
| 李雅的语文是79分, 数学是56分, 英语是31分        |
+----------------------------------------------+
8 rows in set (0.00 sec)

求学生表中学生姓名占用的字节数:

mysql> select length(name), name from student;
+--------------+----------+
| length(name) | name     |
+--------------+----------+
|            6 | 李涛     |
|            6 | 唐僧     |
|            9 | 孙悟空   |
|            9 | 老妖婆   |
|            9 | 红孩儿   |
|           12 | 如来佛祖 |
|            6 | 菩萨     |
|            6 | 李雅     |
+--------------+----------+
8 rows in set (0.00 sec)

将 EMP 表中所有的名字中有 S 的替换成 ‘上海’:

mysql> select replace(ename, 'S', '上海') , ename from EMP;
+-----------------------------+--------+
| replace(ename, 'S', '上海') | ename  |
+-----------------------------+--------+
| 上海MITH                    | SMITH  |
| ALLEN                       | ALLEN  |
| WARD                        | WARD   |
| JONE上海                    | JONES  |
| MARTIN                      | MARTIN |
| BLAKE                       | BLAKE  |
| CLARK                       | CLARK  |
| 上海COTT                    | SCOTT  |
| KING                        | KING   |
| TURNER                      | TURNER |
| ADAM上海                    | ADAMS  |
| JAME上海                    | JAMES  |
| FORD                        | FORD   |
| MILLER                      | MILLER |
+-----------------------------+--------+
14 rows in set (0.00 sec)

截取EMP表中ename字段的第二个到第三个字符:

mysql> select substring(ename, 2, 2), ename from EMP;
+------------------------+--------+
| substring(ename, 2, 2) | ename  |
+------------------------+--------+
| MI                     | SMITH  |
| LL                     | ALLEN  |
| AR                     | WARD   |
| ON                     | JONES  |
| AR                     | MARTIN |
| LA                     | BLAKE  |
| LA                     | CLARK  |
| CO                     | SCOTT  |
| IN                     | KING   |
| UR                     | TURNER |
| DA                     | ADAMS  |
| AM                     | JAMES  |
| OR                     | FORD   |
| IL                     | MILLER |
+------------------------+--------+
14 rows in set (0.00 sec)

以首字母小写的方式显示所有员工的姓名:

mysql> select concat(lcase(substring(ename, 1, 1)), substring(ename, 2)) from EMP;
+------------------------------------------------------------+
| concat(lcase(substring(ename, 1, 1)), substring(ename, 2)) |
+------------------------------------------------------------+
| sMITH                                                      |
| aLLEN                                                      |
| wARD                                                       |
| jONES                                                      |
| mARTIN                                                     |
| bLAKE                                                      |
| cLARK                                                      |
| sCOTT                                                      |
| kING                                                       |
| tURNER                                                     |
| aDAMS                                                      |
| jAMES                                                      |
| fORD                                                       |
| mILLER                                                     |
+------------------------------------------------------------+
14 rows in set (0.00 sec)

2.3 数学函数

abs(number) 绝对值
bin(decimal_num) 十进制转二进制
ceiling(number) 向上取整
conv(number, from_base, to_base) 进制转换
floor(number) 向下取整
format(number, decimal_places) 保留小数位数
hex(decimalNumber) 转换成 16 进制数
rand() 返回随机数浮点值,范围 0 到 1, [0, 1]
mod(numerator, denominator) 余数

绝对值:

mysql> select abs(-100.23);
+--------------+
| abs(-100.23) |
+--------------+
|       100.23 |
+--------------+
1 row in set (0.01 sec)

向上取整:

mysql> select ceiling(23.03);
+----------------+
| ceiling(23.03) |
+----------------+
|             24 |
+----------------+
1 row in set (0.00 sec)

向下取整:

mysql> select floor(23.99);
+--------------+
| floor(23.99) |
+--------------+
|           23 |
+--------------+
1 row in set (0.00 sec)

保留2位小数位数(小数四舍五入):

mysql> select format(12.3456, 2);
+--------------------+
| format(12.3456, 2) |
+--------------------+
| 12.35              |
+--------------------+
1 row in set (0.00 sec)

产生随机数[0,1]

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.6257614471258236 |
+--------------------+
1 row in set (0.00 sec)

2.4 其他常用函数

  • user() :查询当前用户
  • md5 (str) :可以对一个字符串进行 md5 加密,加密后得到一个 32 1为字符串

创建一张表,存用户名和密码:

mysql> create table user(name varchar(30), passwd char(32));

插入数据,密码用md5加密:

mysql> select * from user;
+----------+----------------------------------+
| name     | passwd                           |
+----------+----------------------------------+
| zhangsan | e10adc3949ba59abbe56e057f20f883e |
+----------+----------------------------------+
1 row in set (0.00 sec)

database() 显示当前正在使用的数据库:

mysql> select database();
+------------+
| database() |
+------------+
| mytest     |
+------------+

password()函数,对mysql用户加密
ifnull(val1,val2)如果val1位null,返回val2,否则返回val1的值

猜你喜欢

转载自blog.csdn.net/yubujian_l/article/details/81541170