接着昨天学习的内容,昨天学习了增删改查,查询还有很多操作,今天继续学习。查询的话是可以跟where语句来判断。
因为我昨天建的数据库被人删了。嗯??!今天我们重新创建一个数据库,在终端输入:
mysql -uroot -p123456
create database study charset=utf8;
use study;
我们还是创建一个表格,在终端输入:
create table sanguo(id int primary key,name varchar(16) not null,country enum("魏","蜀","吴"),sex enum("男","女"),attack int not null,defence int not null);
insert into sanguo values(1,"赵云","蜀","男",270,50),(2,"司马懿","魏","男",314,7),(3,"黄月英","蜀","女",230,5),(4,"太史慈","吴","男",290,60),(5,"孙尚香","吴","女",170,10),(6,"诸葛亮","蜀","男",310,11),(7,"张飞","蜀","男",340,70),(8,"张辽","魏","男",190,99),(9,"吕布","魏","男",999,99),(10,"貂蝉","魏","女",99,21),(11,"周瑜","吴","男",350,30),(12,"陆逊","吴","男",290,40),(13,"马超","蜀","男",298,80),(14,"曹操","魏","男",221,60),(15,"刘备","蜀","男",200,40),(16,"孙权","吴","男",160,10);
输完数据之后查询,在终端输入:
mysql> select * from sanguo;
+----+-----------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+-----------+---------+------+--------+---------+
| 1 | 赵云 | 蜀 | 男 | 270 | 50 |
| 2 | 司马懿 | 魏 | 男 | 314 | 7 |
| 3 | 黄月英 | 蜀 | 女 | 230 | 5 |
| 4 | 太史慈 | 吴 | 男 | 290 | 60 |
| 5 | 孙尚香 | 吴 | 女 | 170 | 10 |
| 6 | 诸葛亮 | 蜀 | 男 | 310 | 11 |
| 7 | 张飞 | 蜀 | 男 | 340 | 70 |
| 8 | 张辽 | 魏 | 男 | 190 | 99 |
| 9 | 吕布 | 魏 | 男 | 999 | 99 |
| 10 | 貂蝉 | 魏 | 女 | 99 | 21 |
| 11 | 周瑜 | 吴 | 男 | 350 | 30 |
| 12 | 陆逊 | 吴 | 男 | 290 | 40 |
| 13 | 马超 | 蜀 | 男 | 298 | 80 |
| 14 | 曹操 | 魏 | 男 | 221 | 60 |
| 15 | 刘备 | 蜀 | 男 | 200 | 40 |
| 16 | 孙权 | 吴 | 男 | 160 | 10 |
+----+-----------+---------+------+--------+---------+
16 rows in set (0.01 sec)
而where语句的使用方法如下:
1,我想查询蜀国中攻击力大于200的武将,在终端输入:
select * from sanguo where country="蜀" and attack>200;
+----+-----------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+-----------+---------+------+--------+---------+
| 1 | 赵云 | 蜀 | 男 | 270 | 50 |
| 3 | 黄月英 | 蜀 | 女 | 230 | 5 |
| 6 | 诸葛亮 | 蜀 | 男 | 310 | 11 |
| 7 | 张飞 | 蜀 | 男 | 340 | 70 |
| 13 | 马超 | 蜀 | 男 | 298 | 80 |
+----+-----------+---------+------+--------+---------+
5 rows in set (0.00 sec)
2,如果我想查询攻击在200和400之间的武将,在终端输入:
select * from sanguo where attack between 200 and 400;
+----+-----------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+-----------+---------+------+--------+---------+
| 1 | 赵云 | 蜀 | 男 | 270 | 50 |
| 2 | 司马懿 | 魏 | 男 | 314 | 7 |
| 3 | 黄月英 | 蜀 | 女 | 230 | 5 |
| 4 | 太史慈 | 吴 | 男 | 290 | 60 |
| 6 | 诸葛亮 | 蜀 | 男 | 310 | 11 |
| 7 | 张飞 | 蜀 | 男 | 340 | 70 |
| 11 | 周瑜 | 吴 | 男 | 350 | 30 |
| 12 | 陆逊 | 吴 | 男 | 290 | 40 |
| 13 | 马超 | 蜀 | 男 | 298 | 80 |
| 14 | 曹操 | 魏 | 男 | 221 | 60 |
| 15 | 刘备 | 蜀 | 男 | 200 | 40 |
+----+-----------+---------+------+--------+---------+
11 rows in set (0.00 sec)
3,模糊查询,如果我想查询姓名是三个字的武将,在终端输入:
select * from sanguo where name like "___";
+----+-----------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+-----------+---------+------+--------+---------+
| 2 | 司马懿 | 魏 | 男 | 314 | 7 |
| 3 | 黄月英 | 蜀 | 女 | 230 | 5 |
| 4 | 太史慈 | 吴 | 男 | 290 | 60 |
| 5 | 孙尚香 | 吴 | 女 | 170 | 10 |
| 6 | 诸葛亮 | 蜀 | 男 | 310 | 11 |
+----+-----------+---------+------+--------+---------+
5 rows in set (0.00 sec)
4,如果我想查询姓赵的武将,在终端输入:
select * from sanguo where name like "赵%";
+----+--------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+--------+---------+------+--------+---------+
| 1 | 赵云 | 蜀 | 男 | 270 | 50 |
+----+--------+---------+------+--------+---------+
1 row in set (0.00 sec)
order by
5,如果我想给武将排序,在终端输入:
select * from sanguo order by attack desc;
+----+-----------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+-----------+---------+------+--------+---------+
| 9 | 吕布 | 魏 | 男 | 999 | 99 |
| 11 | 周瑜 | 吴 | 男 | 350 | 30 |
| 7 | 张飞 | 蜀 | 男 | 340 | 70 |
| 2 | 司马懿 | 魏 | 男 | 314 | 7 |
| 6 | 诸葛亮 | 蜀 | 男 | 310 | 11 |
| 13 | 马超 | 蜀 | 男 | 298 | 80 |
| 4 | 太史慈 | 吴 | 男 | 290 | 60 |
| 12 | 陆逊 | 吴 | 男 | 290 | 40 |
| 1 | 赵云 | 蜀 | 男 | 270 | 50 |
| 3 | 黄月英 | 蜀 | 女 | 230 | 5 |
| 14 | 曹操 | 魏 | 男 | 221 | 60 |
| 15 | 刘备 | 蜀 | 男 | 200 | 40 |
| 8 | 张辽 | 魏 | 男 | 190 | 99 |
| 5 | 孙尚香 | 吴 | 女 | 170 | 10 |
| 16 | 孙权 | 吴 | 男 | 160 | 10 |
| 10 | 貂蝉 | 魏 | 女 | 99 | 21 |
+----+-----------+---------+------+--------+---------+
16 rows in set (0.00 sec)
union
6查询的话还有union用法,这个用法默认是去重的,不想去重可以加all,比如我想看一下攻击小于200的魏国武将和所有女性武将,在终端输入:
select * from sanguo where country="魏"and attack<200 union all select * from sanguo where sex="女";
+----+-----------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+-----------+---------+------+--------+---------+
| 8 | 张辽 | 魏 | 男 | 190 | 99 |
| 10 | 貂蝉 | 魏 | 女 | 99 | 21 |
| 3 | 黄月英 | 蜀 | 女 | 230 | 5 |
| 5 | 孙尚香 | 吴 | 女 | 170 | 10 |
| 10 | 貂蝉 | 魏 | 女 | 99 | 21 |
+----+-----------+---------+------+--------+---------+
5 rows in set (0.00 sec)
大家看一下结果,这个结果是两个查询结果显示在一张表格里面,如果刚才不加all,那么会自动去重,那么貂蝉就不会出现两次。
as
7,如果大家觉得表格里面都是英语看不太懂类型的话可以使用as取别名,在终端中输入:
注意,这里 name as 姓名 这句,首先姓名虽然是汉字,但是不用加"",另外这句话不要加()
select name as 姓名,attack as 攻击力 from sanguo;
+-----------+-----------+
| 姓名 | 攻击力 |
+-----------+-----------+
| 赵云 | 270 |
| 司马懿 | 314 |
| 黄月英 | 230 |
| 太史慈 | 290 |
| 孙尚香 | 170 |
| 诸葛亮 | 310 |
| 张飞 | 340 |
| 张辽 | 190 |
| 吕布 | 999 |
| 貂蝉 | 99 |
| 周瑜 | 350 |
| 陆逊 | 290 |
| 马超 | 298 |
| 曹操 | 221 |
| 刘备 | 200 |
| 孙权 | 160 |
+-----------+-----------+
16 rows in set (0.00 sec)
limit
8,如果我想查询魏国攻击力前三的武将,在终端输入:
select * from sanguo where country="魏" order by attack desc limit 3;
+----+-----------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+-----------+---------+------+--------+---------+
| 9 | 吕布 | 魏 | 男 | 999 | 99 |
| 2 | 司马懿 | 魏 | 男 | 314 | 7 |
| 14 | 曹操 | 魏 | 男 | 221 | 60 |
+----+-----------+---------+------+--------+---------+
3 rows in set (0.00 sec)
子查询
9,如果我想查询攻击力比 吴国攻击力最高的武将 还要高的武将信息,在终端输入:
select * from sanguo where attack>(select attack from sanguo where country="吴" order by attack desc limit 1);
+----+--------+---------+------+--------+---------+
| id | name | country | sex | attack | defence |
+----+--------+---------+------+--------+---------+
| 9 | 吕布 | 魏 | 男 | 999 | 99 |
+----+--------+---------+------+--------+---------+
1 row in set (0.00 sec)
!!!!特别注意,子查询和外边的查询可以不是同一个列表
聚合函数
10,如果我想要所有武将平均攻击力,在终端输入:
select avg(attack) from sanguo;
+-------------+
| avg(attack) |
+-------------+
| 295.6875 |
+-------------+
1 row in set (0.00 sec)
常见的函数有: avg(字段名) 平均值
max(字段名) 最大值
min(字段名) 最小值
sum(字段名) 求和
count(字段名) 计算个数(比如count(name)就可以算出来有几个武将名字)
分组聚合
11,如果我想知道每个国家的男性武将的攻击力之和,在终端输入:
select sex,country,sum(attack) from sanguo group by sex,country;
+------+---------+-------------+
| sex | country | sum(attack) |
+------+---------+-------------+
| 男 | 魏 | 1724 |
| 男 | 蜀 | 1418 |
| 男 | 吴 | 1090 |
| 女 | 魏 | 99 |
| 女 | 蜀 | 230 |
| 女 | 吴 | 170 |
+------+---------+-------------+
6 rows in set (0.00 sec)
(⊙o⊙)…这是一个错误的演示(我是故意的…),错误的原因在于讲sex和country都进行分组的话,他们两个属性会两两分组。
select country,sum(attack) from (select * from sanguo where sex="男") as s group by country;
+---------+-------------+
| country | sum(attack) |
+---------+-------------+
| 魏 | 1724 |
| 蜀 | 1418 |
| 吴 | 1090 |
+---------+-------------+
3 rows in set (0.00 sec)
今天内容有点多,没有写完,未完待续,敬请期待…