标签:查询 ike 分组查询 image min() 个数 group 学生 mys
首先创造一个数据库chapter007,选择使用007数据库,在数据库007中创建表,执行创建的表,然后使用INSERT向表中插入八条记录
在查询字段列表中字段的顺序是可以改变的
在SELECT语句中使用星号通配符查询student表中的所有字段
查询表中id为4的学生姓名,使用运算符获取id值为4的数据
查询name为wusong的学生性别
查询grade大于80的学生姓名
查询表中id值为1,2,3的记录
查询表中id值在2和5之间和不在2和5之间的学生姓名
查询表中gender为空值和不为空值的记录
查询表中gender字段的值,查询记录不能重复
查询表中gender和name的字段,使得distinct关键字作用于这两个字段
DISTINCT关键字作用多个字段
带LIKE的关键字查询
查询表中name字段值以字符“w”开始,以字符“g”结束的学生的id
查询表中name字段值包含与不包含字符“y”的学生id
mysql> SELECT*FROM HAHAHA WHERE name LIKE ‘s_n‘;
+----+------+-------+--------+
| id | name | grade | gender |
+----+------+-------+--------+
| 3 | son | 700 | 男 |
| 6 | son | 700 | 女 |
+----+------+-------+--------+
2 rows in set
mysql> SELECT*FROM HAHAHA WHERE name LIKE ‘_n‘;
Empty set
mysql> SELECT*FROM HAHAHA WHERE name LIKE ‘_ang‘;
Empty set
mysql> SELECT*FROM HAHAHA WHERE name LIKE ‘______ang‘;
+----+-----------+-------+--------+
| id | name | grade | gender |
+----+-----------+-------+--------+
| 1 | songjiang | 40 | 男 |
+----+-----------+-------+--------+
1 row in set
mysql> INSERT INTO HAHAHA(name,grade,gender) VALUES(‘SUN%ER‘,95,‘男‘);
Query OK, 1 row affected
mysql> SELECT*FROM HAHAHA WHERE name like ‘%\%%‘;
+----+--------+-------+--------+
| id | name | grade | gender |
+----+--------+-------+--------+
| 7 | SUN%ER | 95 | 男 |
+----+--------+-------+--------+
1 row in set
//带AND关键字的多条件查询
mysql> SELECT id,name,gender FROM HAHAHA WHERE id<3 AND gender=‘女‘;
Empty set
mysql> SELECT id,name,gender FROM HAHAHA WHERE id<3 AND gender=‘男‘;
+----+-----------+--------+
| id | name | gender |
+----+-----------+--------+
| 1 | songjiang | 男 |
| 2 | jiang | 男 |
+----+-----------+--------+
2 rows in set
mysql> SELECT id,name,gender FROM HAHAHA WHERE id in(1,2,3,4)AND name LIKE ‘%ng‘ AND grade<700;
+----+-----------+--------+
| id | name | gender |
+----+-----------+--------+
| 1 | songjiang | 男 |
| 2 | jiang | 男 |
+----+-----------+--------+
2 rows in set
//带OR关键字的多条件查询
mysql> SELECT id,name,gender FROM HAHAHA WHERE id<3 OR gender=‘男‘;
+----+-----------+--------+
| id | name | gender |
+----+-----------+--------+
| 1 | songjiang | 男 |
| 2 | jiang | 男 |
| 3 | son | 男 |
| 4 | sonHH | 男 |
| 7 | SUN%ER | 男 |
+----+-----------+--------+
5 rows in set
mysql> SELECT id,name,gender FROM HAHAHA WHERE name LIKE ‘s%‘ OR grade=700;
+----+-----------+--------+
| id | name | gender |
+----+-----------+--------+
| 1 | songjiang | 男 |
| 3 | son | 男 |
| 4 | sonHH | 男 |
| 5 | SJ | 女 |
| 6 | son | 女 |
| 7 | SUN%ER | 男 |
+----+-----------+--------+
6 rows in set
//OR和AND关键字一起使用的情况
mysql> SELECT id,name,gender FROM HAHAHA WHERE gender=‘女‘ OR gender=‘男‘ AND grade=700;
+----+------+--------+
| id | name | gender |
+----+------+--------+
| 3 | son | 男 |
| 5 | SJ | 女 |
| 6 | son | 女 |
+----+------+--------+
高级查询
mysql> SELECT COUNT(*) FROM HAHAHA;
+----------+
| COUNT(*) |
+----------+
| 7 |
+----------+
1 row in set
//SUM()函数
mysql> SELECT SUM(grade) FROM HAHAHA;
+------------+
| SUM(grade) |
+------------+
| 2705 |
+------------+
1 row in set
//AVG()函数
mysql> SELECT AVG(grade) FROM HAHAHA;
+--------------------+
| AVG(grade) |
+--------------------+
| 386.42857142857144 |
+--------------------+
1 row in set
//MAX()函数
mysql> SELECT MAX(grade) FROM HAHAHA;
+------------+
| MAX(grade) |
+------------+
| 760 |
+------------+
1 row in set
//MIN()函数
mysql> SELECT MIN(grade) FROM HAHAHA;
+------------+
| MIN(grade) |
+------------+
| 10 |
+------------+
1 row in set
mysql> SELECT * FROM HAHAHA ORDER BY grade;
+----+-----------+-------+--------+
| id | name | grade | gender |
+----+-----------+-------+--------+
| 5 | SJ | 10 | 女 |
| 1 | songjiang | 40 | 男 |
| 7 | SUN%ER | 95 | 男 |
| 2 | jiang | 400 | 男 |
| 3 | son | 700 | 男 |
| 6 | son | 700 | 女 |
| 4 | sonHH | 760 | 男 |
+----+-----------+-------+--------+
7 rows in set
//ASC表示按照升序
mysql> SELECT * FROM HAHAHA ORDER BY grade ASC;
+----+-----------+-------+--------+
| id | name | grade | gender |
+----+-----------+-------+--------+
| 5 | SJ | 10 | 女 |
| 1 | songjiang | 40 | 男 |
| 7 | SUN%ER | 95 | 男 |
| 2 | jiang | 400 | 男 |
| 3 | son | 700 | 男 |
| 6 | son | 700 | 女 |
| 4 | sonHH | 760 | 男 |
+----+-----------+-------+--------+
7 rows in set
//DESC表示按照降序
mysql> SELECT * FROM HAHAHA ORDER BY grade DESC;
+----+-----------+-------+--------+
| id | name | grade | gender |
+----+-----------+-------+--------+
| 4 | sonHH | 760 | 男 |
| 3 | son | 700 | 男 |
| 6 | son | 700 | 女 |
| 2 | jiang | 400 | 男 |
| 7 | SUN%ER | 95 | 男 |
| 1 | songjiang | 40 | 男 |
| 5 | SJ | 10 | 女 |
+----+-----------+-------+--------+
7 rows in set
//按照gender字段的升序和grade字段的降序进行排列
mysql> SELECT * FROM HAHAHA ORDER BY gender ASC, grade DESC;
+----+-----------+-------+--------+
| id | name | grade | gender |
+----+-----------+-------+--------+
| 6 | son | 700 | 女 |
| 5 | SJ | 10 | 女 |
| 4 | sonHH | 760 | 男 |
| 3 | son | 700 | 男 |
| 2 | jiang | 400 | 男 |
| 7 | SUN%ER | 95 | 男 |
| 1 | songjiang | 40 | 男 |
+----+-----------+-------+--------+
7 rows in set
//查出HAHAHA表中的所有记录,并按照grade字段进行排序
mysql> SELECT * FROM HAHAHA GROUP BY gender;
+----+-----------+-------+--------+
| id | name | grade | gender |
+----+-----------+-------+--------+
| 5 | SJ | 10 | 女 |
| 1 | songjiang | 40 | 男 |
+----+-----------+-------+--------+
2 rows in set
//分组查询:GROUP BY和聚合函数一起使用
mysql> SELECT COUNT(*),gender FROM HAHAHA GROUP BY gender;
+----------+--------+
| COUNT(*) | gender |
+----------+--------+
| 2 | 女 |
| 5 | 男 |
+----------+--------+
2 rows in set
//分组查询
mysql> SELECT sum(grade),gender FROM HAHAHA GROUP BY gender HAVING SUM(grade)<700;
Empty set
//使用LIMIT限制查询结果的数量
mysql> SELECT * FROM HAHAHA LIMIT 4;
+----+-----------+-------+--------+
| id | name | grade | gender |
+----+-----------+-------+--------+
| 1 | songjiang | 40 | 男 |
| 2 | jiang | 400 | 男 |
| 3 | son | 700 | 男 |
| 4 | sonHH | 760 | 男 |
+----+-----------+-------+--------+
4 rows in set
//查询HAHAHAt表中grade字段值从第2位到第4位的学生
mysql> SELECT * FROM HAHAHA ORDER BY grade DESC LIMIT 2,4;
+----+-----------+-------+--------+
| id | name | grade | gender |
+----+-----------+-------+--------+
| 6 | son | 700 | 女 |
| 2 | jiang | 400 | 男 |
| 7 | SUN%ER | 95 | 男 |
| 1 | songjiang | 40 | 男 |
+----+-----------+-------+--------+
4 rows in set
//函数(列表)查询HAHAHA表中的所有记录,将各个字段值使用下划线“_”连接起来
mysql> SELECT CONCAT(id,‘_‘,name,‘_‘,grade,‘_‘,gender)FROM HAHAHA;
+------------------------------------------+
| CONCAT(id,‘_‘,name,‘_‘,grade,‘_‘,gender) |
+------------------------------------------+
| 1_songjiang_40_男 |
| 2_jiang_400_男 |
| 3_son_700_男 |
| 4_sonHH_760_男 |
| 5_SJ_10_女 |
| 6_son_700_女 |
| 7_SUN%ER_95_男 |
+------------------------------------------+
7 rows in set
//查询HAHAHA表中的id和gender字段值,如果gender字段的值为“男”则返回1
mysql> SELECT id,IF(gender=‘男‘,1,0) FROM HAHAHA;
+----+---------------------+
| id | IF(gender=‘男‘,1,0) |
+----+---------------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 0 |
| 6 | 0 |
| 7 | 1 |
+----+---------------------+
7 rows in set
//为表取别名
mysql> SELECT * FROM HAHAHA AS a WHERE a.gender=‘女‘;
+----+------+-------+--------+
| id | name | grade | gender |
+----+------+-------+--------+
| 5 | SJ | 10 | 女 |
| 6 | son | 700 | 女 |
+----+------+-------+--------+
2 rows in set
//查询HAHAHA表中的所有记录的name和gender字段值,并为这两个字段其别名stu_name和stu_gender
mysql> SELECT name AS stu_name,gender stu_gender FROM HAHAHA;
+-----------+------------+
| stu_name | stu_gender |
+-----------+------------+
| songjiang | 男 |
| jiang | 男 |
| son | 男 |
| sonHH | 男 |
| SJ | 女 |
| son | 女 |
| SUN%ER | 男 |
+-----------+------------+
7 rows in set
标签:查询 ike 分组查询 image min() 个数 group 学生 mys
原文地址:https://www.cnblogs.com/bjyxszdxzwyb/p/11919356.html