(十一)使用select显示
输入:
select * fromsales_rep where commission>1;
显示:
mysql>select * from sales_rep where commission>1;
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 1 | Rive | Sol | 10 |
| 2 | John | char | 11 |
| 3 | Jop | Peter | 12 |
| 4 | dong | wang | 20 |
| 6 | bbb | BB | 2 |
| 7 | ccc | CC | 3 |
| 8 | ddd | DDD | 4 |
| 9 | ee | EEEE | 5 |
| 10 | ff | FFF | 6 |
+-----------------+---------+------------+------------+
9rows in set (0.00 sec)
解释:
①select:表示使用select语句
②*:表示通配符(显示所有的)。可以用某一项替换,如surname,则只显示surname这一项;
③from sales_rep:指从表名为sales_rep的这一张表读取内容;
④where commission>1:指的是,读取表内commission这一个属性大于1的所有项;
也可以改为:where first_name='cc' :那么就只显示first_name的值是cc的项了(注意需要加引号)(并且不区分大小写,注意,mysql几乎是不区分大小写的)
⑤;:以分号为结尾。
(十二)where从句
基于(十一)
输入:
select * fromsales_rep where first_name!='cc' or first_name="cc";
显示:
mysql>select * from sales_rep where first_name!='cc' or first_name="cc";
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 1 | Rive | Sol | 10 |
| 2 | John | char | 11 |
| 3 | Jop | Peter | 12 |
| 4 | dong | wang | 20 |
| 5 | aaa | AAA | 1 |
| 6 | bbb | BB | 2 |
| 7 | ccc | CC | 3 |
| 8 | ddd | DDD | 4 |
| 9 | ee | EEEE | 5 |
| 10 | ff | FFF | 6 |
+-----------------+---------+------------+------------+
10rows in set (0.00 sec)
解释:
①or:前后各有一条语句first_name!='cc'和first_name="cc",
②or的效果是两条语句只要有一条成立,便符合条件。由于一个内容要么是cc,要么不是cc(必然的),所以每一项都会成立(因此相当于显示除了null的)。
③and:也可以用and来连接,表示两条语句都符合的才行。(有一条不符合就不成立)。
④多个or和and联用:支持联用。
or的联用:mysql> select * fromsales_rep where first_name='bb'or first_name="cc"orfirst_name="aaa"; 三者符合一个的就可以。
and联用:由此可推,可以多个and联用。
也可以or和and混合使用,但为了避免理解出错,请使用括号来区分层级。
(十三)通配符%
%是一个通配符,类似*,表示0个或者多个字符。
输入:
select * fromsales_rep where first_name like "%c%";
显示:
mysql>select * from sales_rep where first_name like "%c%";
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 2 | John | char | 11 |
| 7 | ccc | CC | 3 |
+-----------------+---------+------------+------------+
2rows in set (0.00 sec)
效果:
first_name属性里有c字母的项,全部都是符合条件的。
(十四)分类
排序:(正序)
输入:
select * fromsales_rep order by first_name;
显示:
mysql>select * from sales_rep order by first_name;
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 0 | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL |
| 5 | aaa | AAA | 1 |
| 6 | bbb | BB | 2 |
| 7 | ccc | CC | 3 |
| 2 | John | char | 11 |
| 8 | ddd | DDD | 4 |
| 9 | ee | EEEE | 5 |
| 10 | ff | FFF | 6 |
| 3 | Jop | Peter | 12 |
| 1 | Rive | Sol | 10 |
| 4 | dong | wang | 20 |
+-----------------+---------+------------+------------+
15rows in set (0.00 sec)
效果:
在first_name项,以字母顺序进行排序(会显示null项);
排序:(多重排序)
输入:
select * fromsales_rep order by surname,first_name;
显示:
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 0 | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL |
| 5 | aaa | AAA | 1 |
| 5 | bbb | AAA | 5 |
| 6 | bbb | BB | 2 |
| 7 | ccc | CC | 3 |
| 8 | ddd | DDD | 4 |
| 4 | dong | wang | 20 |
| 9 | ee | EEEE | 5 |
| 10 | ff | FFF | 6 |
| 2 | John | char | 11 |
| 3 | Jop | Peter | 12 |
| 1 | Rive | Sol | 10 |
+-----------------+---------+------------+------------+
16 rows in set (0.00 sec)
效果说明:
①多了一个surname作为参数。效果是,先以第一个参数所在的项为顺序进行排序显示,假如第一个一样,以第二个参数进行排序;
②由此类推,可以有更多项。
排序:(倒序)
输入:
select * fromsales_rep order by first_name desc;
显示:
mysql>select * from sales_rep order by first_name desc;
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 4 | dong | wang | 20 |
| 1 | Rive | Sol | 10 |
| 3 | Jop | Peter | 12 |
| 10 | ff | FFF | 6 |
| 9 | ee | EEEE | 5 |
| 8 | ddd | DDD | 4 |
| 2 | John | char | 11 |
| 7 | ccc | CC | 3 |
| 6 | bbb | BB | 2 |
| 5 | bbb | AAA | 5 |
| 5 | aaa | AAA | 1 |
| 0 | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL |
+-----------------+---------+------------+------------+
16rows in set (0.00 sec)
区别:
在正序的命令后面加一个desc即可。
排序:(正序关键字)
假如需要第一个参数按正序,相同的情况下,第二个参数按反序进行排列。
输入:
select * fromsales_rep order by surname asc, employes_number desc;
显示:
mysql>select * from sales_rep order by surname asc, employes_number desc;
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 10 | NULL | NULL | NULL |
| 9 | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL |
| 0 | NULL | NULL | NULL |
| 0 | NULL | NULL | NULL |
| 5 | aaa | AAA | 1 |
| 6 | bbb | BB | 2 |
| 5 | bbb | AAA | 5 |
| 7 | ccc | CC | 3 |
| 8 | ddd | DDD | 4 |
| 4 | dong | wang | 20 |
| 9 | ee | EEEE | 5 |
| 10 | ff | FFF | 6 |
| 2 | John | char | 11 |
| 3 | Jop | Peter | 12 |
| 1 | Rive | Sol | 10 |
+-----------------+---------+------------+------------+
16rows in set (0.00 sec)
(十五)限制返回的项数LIMIT
输出行数限制:
输入:(在排序:(正序关键字)基础上修改)
select * from sales_rep order by surname asc, employes_number desclimit 1;
显示:
mysql>select * from sales_rep order by surname asc, employes_number desc limit
1;
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 10 | NULL | NULL | NULL |
+-----------------+---------+------------+------------+
1row in set (0.00 sec)
解释:
①limit可以设置为0(虽然不显示内容,但是可以用于在大的数据库中进行测试查询);
②limit后面的数字决定了返回的行数;
输出行数限制+偏移限制:
输入:
select * fromsales_rep order by surname asc, employes_number desc limit 1,2;
显示:
mysql>select * from sales_rep order by surname asc, employes_number desc limit
1,2;
+-----------------+---------+------------+------------+
|employes_number | surname | first_name | commission |
+-----------------+---------+------------+------------+
| 9 | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL |
+-----------------+---------+------------+------------+
2rows in set (0.00 sec)
解释:
①参数1表示从开始偏移几行(即从理论应该显示的结果的第几行开始显示);
②参数2表示显示几行。
③1,2表示从第2行(偏移1)开始显示,显示2行
(十六)返回最大值MAX()
输入:
selectmax(commission) from sales_rep;
显示:
mysql>select max(commission) from sales_rep;
+-----------------+
|max(commission) |
+-----------------+
| 20 |
+-----------------+
1row in set (0.00 sec)
效果:
返回commission这一项中最大的值。(对字母也有效)
(十七)防止显示重复记录distinct
输入:
selectdistinct first_name from sales_rep;
显示:
mysql>select distinct first_name from sales_rep;
+------------+
|first_name |
+------------+
|Sol |
|char |
|Peter |
|wang |
|AAA |
|BB |
|CC |
|NULL |
|DDD |
|EEEE |
|FFF |
+------------+
11rows in set (0.00 sec)
效果:
①原本都个NULL,这里只显示一个了;
②原本2个AAA,这里只显示一个了;
(十八)计数count()
显示有效数据的行数(不显示null):
输入:
selectcount(first_name) from sales_rep;
显示:
mysql>select count(first_name) from sales_rep;
+-------------------+
|count(first_name) |
+-------------------+
| 11 |
+-------------------+
1row in set (0.00 sec)
效果:
①除了null都显示;
显示所有数据的行数(貌似除非都是null,否则就显示):
输入:
selectcount(*) from sales_rep;
显示:
mysql>select count(*) from sales_rep;
+----------+
|count(*) |
+----------+
| 16 |
+----------+
1row in set (0.00 sec)
效果:
①即使有一项是null(其他项不是),也被计算在内;
显示行数:(重复项算一项,使用distinct)
输入:
selectcount(distinct first_name) from sales_rep;
显示:
mysql>select count(distinct first_name) from sales_rep;
+----------------------------+
|count(distinct first_name) |
+----------------------------+
| 10 |
+----------------------------+
1row in set (0.00 sec)
效果:
①该项(first_name)中如果有重复的,记作一项。
(十九)平均、最小、总和
平均:avg()
最小:min()
总和:sum()
输入:
selectsum(commission) from sales_rep;
显示:
mysql>select sum(commission) from sales_rep;
+-----------------+
|sum(commission) |
+-----------------+
| 79 |
+-----------------+
1row in set (0.00 sec)
效果:
①三个方法类似,结果不同;
②对字符串无效
(二十)对计算结果修改(不影响原数据)
输入:
selectemployes_number ,surname, commission+100 from sales_rep;
显示:
mysql>select employes_number ,surname, commission+100 from sales_rep;
+-----------------+---------+----------------+
|employes_number | surname | commission+100 |
+-----------------+---------+----------------+
| 1 | Rive | 110 |
| 2 | John | 111 |
| 3 | Jop | 112 |
| 4 | dong | 120 |
| 5 | aaa | 101 |
| 6 | bbb | 102 |
| 7 | ccc | 103 |
| 8 | NULL | NULL |
| 9 | NULL | NULL |
| 10 | NULL | NULL |
| 0 | NULL | NULL |
| 8 | ddd | 104 |
| 9 | ee | 105 |
| 10 | ff | 106 |
| 0 | NULL | NULL |
| 5 | bbb | 105 |
+-----------------+---------+----------------+
16rows in set (0.00 sec)
效果:
①指定项显示结果 + 100,(commission+100);
②显示选择的项(employes_number ,surname,commission);
③不影响原数据;
(二十一)删除记录
输入:
delete fromsales_rep where surname="aaa";
显示:
mysql> delete from sales_rep where surname="aaa";
Query OK, 1 row affected (0.06 sec)
效果:
①删除值为null的项:delete fromsales_rep where surname is null;
②删除记录将删除所有符合要求的(所以尽量使用唯一项,例如类似id的东西);
③假如输入delete from sales_rep 将删除所有项。
(二十二)修改记录
输入:
updatesales_rep set commission = 20 where employes_number=1;
显示:
mysql> update sales_rep set commission = 20 where employes_number=1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
效果:
①employes_number的值是1的项,其commission的值被更改为20。