先编写两个脚本,生成一个test
库,并在库中分别添加两个表,一个students
,一个teachers
对于students
表:"studetns.sh"
#/bin/bash `mysql -e "CREATE DATABASE IF NOT EXISTS $1 CHARACTER SET = 'gbk' COLLATE = 'gbk_chinese_ci'"` //如果没有$1数据库,则创建一个$1数据库, `mysql -e "CREATE TABLE IF NOT EXISTS $1.$2 (sid TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(30) NOT NULL,age TINYINT UNSIGNED NOT NULL,sex ENUM('女','男'),cid1 TINYINT UNSIGNED,cid2 TINYINT UNSIGNED,tid TINYINT UNSIGNED,time_start TIMESTAMP ) ENGINE=InnoDB;" ` //如果没有$1.$2表,并有sid,name,age,sex,cid1,cid2,tid,time_start //插入随机数据,填充$2表 while [ 1 ] do ##生成随机姓名## name='' for i in {1..3} do name=$name$(tr -dc a-z < /dev/urandom | head -c1) done #### age=$(shuf -i 16-28 -n 1) cid1=$(shuf -i 1-10 -n 1) cid2=$(shuf -i 1-10 -n 1) tid=$(shuf -i 1-5 -n 1) ##性别随机生成## pool=(1 2) num=${#pool[*]} sex=${pool[$((RANDOM%num))]} case $sex in 1) sex="女";; 2) sex="男";; esac #### `mysql -e "INSERT INTO $1.$2(name,age,sex,cid1,cid2,tid) VALUES('$name',$age,'$sex',$cid1,$cid2,$tid)"` done
执行students.sh
[root@mysql mysql]# bash students.sh test students //test是$1,是库;students是$2,是表 //在mysql交互模式下,可以查看数据信息 mysql> select * from students; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 1 | gtm | 23 | 女 | 8 | 7 | 2 | 2018-05-18 23:00:12 | | 2 | wlz | 23 | 男 | 1 | 2 | 2 | 2018-05-18 23:00:12 | | 3 | mjd | 16 | 女 | 3 | 3 | 5 | 2018-05-18 23:00:12 | | 4 | ncq | 23 | 男 | 9 | 10 | 3 | 2018-05-18 23:00:12 | | 5 | fmp | 21 | 男 | 7 | 1 | 1 | 2018-05-18 23:00:12 | | 6 | umh | 21 | 女 | 9 | 4 | 3 | 2018-05-18 23:00:12 | | 7 | kxo | 26 | 女 | 3 | 4 | 4 | 2018-05-18 23:00:12 | | 8 | vjp | 17 | 女 | 4 | 2 | 4 | 2018-05-18 23:00:13 | | 9 | uos | 28 | 女 | 5 | 7 | 2 | 2018-05-18 23:00:13 | | 10 | fzs | 20 | 男 | 7 | 6 | 4 | 2018-05-18 23:00:13 | | 11 | duc | 24 | 女 | 2 | 6 | 1 | 2018-05-18 23:00:13 | | 12 | uqq | 18 | 女 | 6 | 3 | 2 | 2018-05-18 23:00:13 | | 13 | tzi | 20 | 男 | 6 | 8 | 2 | 2018-05-18 23:00:13 | | 14 | fki | 17 | 男 | 10 | 4 | 5 | 2018-05-18 23:00:13 | | 15 | dnm | 19 | 男 | 5 | 9 | 2 | 2018-05-18 23:00:13 | | 16 | qeh | 27 | 女 | 9 | 4 | 5 | 2018-05-18 23:00:13 | | 17 | ymt | 20 | 女 | 4 | 3 | 4 | 2018-05-18 23:00:13 | | 18 | byd | 28 | 男 | 1 | 1 | 1 | 2018-05-18 23:00:14 | | 19 | vqf | 17 | 男 | 9 | 10 | 5 | 2018-05-18 23:00:14 | //执行完后,可以看到生成了一个test库,一个students表,表中内容已经被填充到210多行(primary key限制最多可以加255行)
对于teachers
表:"teachers.sh"
[root@mysql mysql]# cat teachers.sh #/bin/bash `mysql -e "CREATE DATABASE IF NOT EXISTS $1 CHARACTER SET = 'gbk' COLLATE = 'gbk_chinese_ci';"` //如果不存在$1的数据库,则创建 `mysql -e "CREATE TABLE IF NOT EXISTS $1.$2 (tid TINYINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,name VARCHAR(30) NOT NULL,subject VARCHAR(30),age TINYINT NOT NULL);"` //创建一个$1.$2的表 //向表中添加数据 for k in 语文 数学 英语 历史 地理 政治 物理 化学 生物 体育 do subject=$k age=$(shuf -i 29-55 -n 1) name='' for j in {1..3} do name=$name$(tr -dc a-z < /dev/urandom | head -c1) done # echo $subject # echo $age # echo $name `mysql -e "INSERT INTO $1.$2(name,subject,age) VALUES('$name','$subject',$age);"` done
执行teachers.sh
脚本
[root@mysql mysql]# bash teachers.sh test teachers //test是$1,是库;teachers是$2,是表 //在mysql交互模式下,查看数据信息 mysql> select * from teachers; +-----+------+---------+-----+ | tid | name | subject | age | +-----+------+---------+-----+ | 1 | hjh | 语文 | 53 | | 2 | fma | 数学 | 55 | | 3 | wic | 英语 | 44 | | 4 | xta | 历史 | 54 | | 5 | lwi | 地理 | 30 | | 6 | qxw | 政治 | 30 | | 7 | jpv | 物理 | 32 | | 8 | xvi | 化学 | 34 | | 9 | isr | 生物 | 51 | | 10 | lwq | 体育 | 33 | +-----+------+---------+-----+ 10 rows in set (0.00 sec)
这里只使用students
表
使用SELECT
对单表所有字段进行查询:
mysql> select * from students; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 1 | gtm | 23 | 女 | 8 | 7 | 2 | 2018-05-18 23:00:12 | | 2 | wlz | 23 | 男 | 1 | 2 | 2 | 2018-05-18 23:00:12 | | 3 | mjd | 16 | 女 | 3 | 3 | 5 | 2018-05-18 23:00:12 | | 4 | ncq | 23 | 男 | 9 | 10 | 3 | 2018-05-18 23:00:12 | | 5 | fmp | 21 | 男 | 7 | 1 | 1 | 2018-05-18 23:00:12 | | 6 | umh | 21 | 女 | 9 | 4 | 3 | 2018-05-18 23:00:12 | | 7 | kxo | 26 | 女 | 3 | 4 | 4 | 2018-05-18 23:00:12 | | 8 | vjp | 17 | 女 | 4 | 2 | 4 | 2018-05-18 23:00:13 | | 9 | uos | 28 | 女 | 5 | 7 | 2 | 2018-05-18 23:00:13 | | 10 | fzs | 20 | 男 | 7 | 6 | 4 | 2018-05-18 23:00:13 | //下面还有好多
如果要查询某一个或者几个字段,则将*改为对应字段的名称。这种方式称之为投影查询
mysql> SELECT name,age,cid1 FROM students; +------+-----+------+ | name | age | cid1 | +------+-----+------+ | gtm | 23 | 8 | | wlz | 23 | 1 | | mjd | 16 | 3 | | ncq | 23 | 9 | | fmp | 21 | 7 | | umh | 21 | 9 | | kxo | 26 | 3 | | vjp | 17 | 4 | //下面还有好多
加入DISTINCT
关键字是为了解决冗余问题,如当我们只想查询cid1
有多少种,则可以使用DISTINCT
关键字
mysql> SELECT DISTINCT cid1 FROM students; +------+ | cid1 | +------+ | 8 | | 1 | | 3 | | 9 | | 7 | | 4 | | 5 | | 2 | | 6 | | 10 | +------+ 10 rows in set (0.01 sec)
如果要查询所有字段的某些行,可以使用WHERE进行过滤。这种方式称之为选择查询
mysql> SELECT * FROM students WHERE cid1 < 2; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 2 | wlz | 23 | 男 | 1 | 2 | 2 | 2018-05-18 23:00:12 | | 18 | byd | 28 | 男 | 1 | 1 | 1 | 2018-05-18 23:00:14 | | 40 | zzl | 18 | 男 | 1 | 4 | 2 | 2018-05-18 23:00:16 | | 50 | wtj | 21 | 女 | 1 | 4 | 2 | 2018-05-18 23:00:17 | | 87 | pjo | 24 | 女 | 1 | 5 | 4 | 2018-05-18 23:00:21 | | 89 | yxi | 24 | 女 | 1 | 10 | 2 | 2018-05-18 23:00:21 | | 98 | ows | 28 | 女 | 1 | 1 | 5 | 2018-05-18 23:00:23 | | 99 | jho | 27 | 男 | 1 | 8 | 2 | 2018-05-18 23:00:23 | | 103 | kpz | 23 | 女 | 1 | 9 | 1 | 2018-05-18 23:00:23 | | 104 | jay | 19 | 男 | 1 | 6 | 1 | 2018-05-18 23:00:23 | | 105 | zdd | 25 | 男 | 1 | 9 | 4 | 2018-05-18 23:00:24 | | 111 | jxi | 22 | 女 | 1 | 6 | 3 | 2018-05-18 23:00:24 | | 122 | bfa | 22 | 男 | 1 | 1 | 5 | 2018-05-18 23:00:26 | | 137 | nwc | 21 | 男 | 1 | 3 | 2 | 2018-05-18 23:00:28 | | 163 | nbs | 16 | 女 | 1 | 4 | 2 | 2018-05-18 23:00:30 | | 177 | dcb | 24 | 男 | 1 | 8 | 2 | 2018-05-18 23:00:32 | | 183 | cej | 20 | 女 | 1 | 9 | 3 | 2018-05-18 23:00:32 | | 200 | ygs | 27 | 男 | 1 | 10 | 4 | 2018-05-18 23:00:34 | | 214 | uwe | 27 | 女 | 1 | 1 | 3 | 2018-05-18 23:00:36 | +-----+------+-----+------+------+------+------+---------------------+ 19 rows in set (0.01 sec) //可以看到,显示的全部是cid1小于2的数据信息
WHERE
子句用来指定一个布尔关系表达式(为真为假或者一个范围),通常使用=,<,>,<=,>=,!
等。在做数值比较时,不加引号;在做字符串比较时,要添加引号;这种查询方式很不理想,因为我们无法有效使用索引
当然这些逻辑判断通常也可以组合起来使用,称之为组合表达式
mysql> SELECT * FROM students WHERE cid1<2 && cid2<3; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 2 | wlz | 23 | 男 | 1 | 2 | 2 | 2018-05-18 23:00:12 | | 18 | byd | 28 | 男 | 1 | 1 | 1 | 2018-05-18 23:00:14 | | 98 | ows | 28 | 女 | 1 | 1 | 5 | 2018-05-18 23:00:23 | | 122 | bfa | 22 | 男 | 1 | 1 | 5 | 2018-05-18 23:00:26 | | 214 | uwe | 27 | 女 | 1 | 1 | 3 | 2018-05-18 23:00:36 | +-----+------+-----+------+------+------+------+---------------------+ mysql> SELECT name,age,sex,tid FROM students WHERE cid1<5 && cid2=3; +------+-----+------+------+ | name | age | sex | tid | +------+-----+------+------+ | mjd | 16 | 女 | 5 | | ymt | 20 | 女 | 4 | | loe | 16 | 男 | 4 | | vte | 20 | 男 | 5 | | box | 26 | 女 | 1 | | nce | 20 | 男 | 2 | | nwc | 21 | 男 | 2 | | mio | 20 | 女 | 2 | | maj | 18 | 女 | 3 | +------+-----+------+------+ 9 rows in set (0.00 sec)
LIKE
也是WHERE
的条件筛选的一种方式,具体类似于模糊查找,如:
like a%
就是以a
开头后面任意长度
like a_
就是以a
开头后面跟一个字符
mysql> SELECT * FROM students WHERE name LIKE 'a%'; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 34 | aru | 23 | 女 | 10 | 1 | 2 | 2018-05-18 23:00:15 | | 39 | axa | 28 | 女 | 7 | 3 | 5 | 2018-05-18 23:00:16 | | 102 | ahj | 21 | 男 | 5 | 3 | 3 | 2018-05-18 23:00:23 | | 110 | ani | 28 | 女 | 6 | 6 | 5 | 2018-05-18 23:00:24 | | 150 | aql | 19 | 男 | 9 | 5 | 5 | 2018-05-18 23:00:29 | | 174 | akv | 17 | 女 | 8 | 10 | 3 | 2018-05-18 23:00:31 | | 204 | aqq | 20 | 男 | 3 | 1 | 3 | 2018-05-18 23:00:35 | | 210 | afn | 28 | 女 | 4 | 9 | 1 | 2018-05-18 23:00:35 | | 213 | arp | 20 | 男 | 10 | 4 | 3 | 2018-05-18 23:00:36 | +-----+------+-----+------+------+------+------+---------------------+ mysql> SELECT * FROM students WHERE name LIKE 'a_v'; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 174 | akv | 17 | 女 | 8 | 10 | 3 | 2018-05-18 23:00:31 | +-----+------+-----+------+------+------+------+---------------------+ 1 row in set (0.00 sec)
mysql
查询支持正则表达式,注意,当使用正则表达式时,里面的内容就是正则筛选规则
如:查询students
表中name
以a k
开头的内容:
mysql> SELECT * FROM students WHERE name RLIKE '^[ax].*'; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 34 | aru | 23 | 女 | 10 | 1 | 2 | 2018-05-18 23:00:15 | | 36 | xiy | 22 | 男 | 4 | 6 | 2 | 2018-05-18 23:00:16 | | 39 | axa | 28 | 女 | 7 | 3 | 5 | 2018-05-18 23:00:16 | | 58 | xja | 17 | 男 | 8 | 7 | 4 | 2018-05-18 23:00:18 | | 102 | ahj | 21 | 男 | 5 | 3 | 3 | 2018-05-18 23:00:23 | | 110 | ani | 28 | 女 | 6 | 6 | 5 | 2018-05-18 23:00:24 | | 131 | xvw | 21 | 女 | 10 | 6 | 1 | 2018-05-18 23:00:27 | | 133 | xzf | 17 | 女 | 4 | 7 | 2 | 2018-05-18 23:00:27 | | 149 | xnd | 22 | 女 | 6 | 5 | 4 | 2018-05-18 23:00:29 | | 150 | aql | 19 | 男 | 9 | 5 | 5 | 2018-05-18 23:00:29 | | 174 | akv | 17 | 女 | 8 | 10 | 3 | 2018-05-18 23:00:31 | | 204 | aqq | 20 | 男 | 3 | 1 | 3 | 2018-05-18 23:00:35 | | 210 | afn | 28 | 女 | 4 | 9 | 1 | 2018-05-18 23:00:35 | | 213 | arp | 20 | 男 | 10 | 4 | 3 | 2018-05-18 23:00:36 | | 215 | xxo | 28 | 男 | 8 | 8 | 5 | 2018-05-18 23:00:36 | +-----+------+-----+------+------+------+------+---------------------+ 15 rows in set (0.00 sec)
离散取值查询的时候,我们可以使用列表:IN( , , )。
如:查找cid1
为1 2
的行信息
mysql> SELECT * FROM students WHERE cid1 IN(1,2); +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 2 | wlz | 23 | 男 | 1 | 2 | 2 | 2018-05-18 23:00:12 | | 11 | duc | 24 | 女 | 2 | 6 | 1 | 2018-05-18 23:00:13 | | 18 | byd | 28 | 男 | 1 | 1 | 1 | 2018-05-18 23:00:14 | | 40 | zzl | 18 | 男 | 1 | 4 | 2 | 2018-05-18 23:00:16 | | 45 | ztm | 22 | 女 | 2 | 4 | 4 | 2018-05-18 23:00:17 | | 46 | loe | 16 | 男 | 2 | 3 | 4 | 2018-05-18 23:00:17 | | 50 | wtj | 21 | 女 | 1 | 4 | 2 | 2018-05-18 23:00:17 | | 51 | ihi | 24 | 男 | 2 | 7 | 4 | 2018-05-18 23:00:17 | | 59 | izc | 17 | 女 | 2 | 7 | 5 | 2018-05-18 23:00:18 | | 69 | cxo | 28 | 女 | 2 | 2 | 5 | 2018-05-18 23:00:19 | | 87 | pjo | 24 | 女 | 1 | 5 | 4 | 2018-05-18 23:00:21 | | 89 | yxi | 24 | 女 | 1 | 10 | 2 | 2018-05-18 23:00:21 | //还有很多。。。
因为对NULL
的查询是一个很危险的动作,所以这时候要使用关键字IS
mysql> INSERT INTO students (name,age,sex) VALUES('fsx',24,'男'); Query OK, 1 row affected (0.14 sec) //先插入一个cid1为空的行信息 mysql> SELECT * FROM students WHERE cid1 IS NULL; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 216 | fsx | 24 | 男 | NULL | NULL | NULL | 2018-05-19 00:12:30 | +-----+------+-----+------+------+------+------+---------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM students WHERE cid1=NULL; Empty set (0.00 sec) //这是不对的做法
如果我们想对查询的结果进行排序,可以使用ORDER BY
排序的必要性
数据存储在磁盘上有三种方式:堆方式、顺序方式、HASH方式,如果数据本来就是乱的存放在磁盘上,则要按顺序读取数据十分消耗资源,所以在存储时,让它进行顺序存储是很必要的,所以排序是很重要的
如:对students
表中的内容,按照name
排序后输出:
mysql> SELECT * FROM students ORDER BY name; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 210 | afn | 28 | 女 | 4 | 9 | 1 | 2018-05-18 23:00:35 | | 102 | ahj | 21 | 男 | 5 | 3 | 3 | 2018-05-18 23:00:23 | | 174 | akv | 17 | 女 | 8 | 10 | 3 | 2018-05-18 23:00:31 | | 110 | ani | 28 | 女 | 6 | 6 | 5 | 2018-05-18 23:00:24 | | 150 | aql | 19 | 男 | 9 | 5 | 5 | 2018-05-18 23:00:29 | | 204 | aqq | 20 | 男 | 3 | 1 | 3 | 2018-05-18 23:00:35 | | 213 | arp | 20 | 男 | 10 | 4 | 3 | 2018-05-18 23:00:36 | | 34 | aru | 23 | 女 | 10 | 1 | 2 | 2018-05-18 23:00:15 | | 39 | axa | 28 | 女 | 7 | 3 | 5 | 2018-05-18 23:00:16 | | 181 | bcc | 28 | 男 | 2 | 6 | 2 | 2018-05-18 23:00:32 | | 52 | bci | 19 | 女 | 7 | 4 | 4 | 2018-05-18 23:00:17 | | 122 | bfa | 22 | 男 | 1 | 1 | 5 | 2018-05-18 23:00:26 | //可以看到,输出的行数据都是按照name进行排序后的结果
可以对字段起别名后进行输出信息:
mysql> SELECT name,age,sex AS '性别' FROM students WHERE cid1<2; +------+-----+--------+ | name | age | 性别 | //可以看到字段信息中,sex变成了性别 +------+-----+--------+ | wlz | 23 | 男 | | byd | 28 | 男 | | zzl | 18 | 男 | | wtj | 21 | 女 | | pjo | 24 | 女 | | yxi | 24 | 女 | | ows | 28 | 女 | | jho | 27 | 男 | | kpz | 23 | 女 | | jay | 19 | 男 | | zdd | 25 | 男 | | jxi | 22 | 女 | | bfa | 22 | 男 | | nwc | 21 | 男 | | nbs | 16 | 女 | | dcb | 24 | 男 | | cej | 20 | 女 | | ygs | 27 | 男 | | uwe | 27 | 女 | +------+-----+--------+ 19 rows in set (0.00 sec)
有时候我们只想查前几个,或者从第a个到第b个的行数据信息,此时就可以使用LIMIT
具体用法:LIMIT [OFF_SET,] COUNT
OFF_SET表示偏移量,COUNT表示取的个数:
如:取前三行信息:
mysql> SELECT * FROM students LIMIT 3; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 1 | gtm | 23 | 女 | 8 | 7 | 2 | 2018-05-18 23:00:12 | | 2 | wlz | 23 | 男 | 1 | 2 | 2 | 2018-05-18 23:00:12 | | 3 | mjd | 16 | 女 | 3 | 3 | 5 | 2018-05-18 23:00:12 | +-----+------+-----+------+------+------+------+---------------------+
如:取第5到第9行数据信息
mysql> SELECT * FROM students LIMIT 4,5; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 5 | fmp | 21 | 男 | 7 | 1 | 1 | 2018-05-18 23:00:12 | | 6 | umh | 21 | 女 | 9 | 4 | 3 | 2018-05-18 23:00:12 | | 7 | kxo | 26 | 女 | 3 | 4 | 4 | 2018-05-18 23:00:12 | | 8 | vjp | 17 | 女 | 4 | 2 | 4 | 2018-05-18 23:00:13 | | 9 | uos | 28 | 女 | 5 | 7 | 2 | 2018-05-18 23:00:13 | +-----+------+-----+------+------+------+------+---------------------+ 5 rows in set (0.00 sec)
聚合:对表中的字段进行条件计算,对数据进行特殊操作过滤的动作。
如:求某一字段所有值的平均值,就要使用
AVG
关键字。其它聚合操作还有MIN、MAX、SUM、COUNT
如:求students
表中cid1
的平均值
mysql> SELECT AVG(cid1) FROM students; +-----------+ | AVG(cid1) | +-----------+ | 5.7860 | +-----------+ 1 row in set (0.00 sec)
如:求students
表中的tid
最大的行信息
mysql> SELECT * FROM students WHERE tid=(SELECT MAX(tid) FROM students); +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 3 | mjd | 16 | 女 | 3 | 3 | 5 | 2018-05-18 23:00:12 | | 14 | fki | 17 | 男 | 10 | 4 | 5 | 2018-05-18 23:00:13 | | 16 | qeh | 27 | 女 | 9 | 4 | 5 | 2018-05-18 23:00:13 | | 19 | vqf | 17 | 男 | 9 | 10 | 5 | 2018-05-18 23:00:14 | | 26 | ynb | 24 | 女 | 3 | 2 | 5 | 2018-05-18 23:00:14 | | 30 | vms | 27 | 女 | 7 | 1 | 5 | 2018-05-18 23:00:15 | | 38 | wpy | 22 | 女 | 6 | 5 | 5 | 2018-05-18 23:00:16 | | 39 | axa | 28 | 女 | 7 | 3 | 5 | 2018-05-18 23:00:16 | //tid最大为5,当为5时,有很多行信息
实现对某一字段进行分组,将一个字段的所有数据都分成若干组(取决于该字段有多少种不同的取值),分组的意义时在后续的聚合运算
mysql> SELECT * FROM students GROUP BY cid2; +-----+------+-----+------+------+------+------+---------------------+ | sid | name | age | sex | cid1 | cid2 | tid | time_start | +-----+------+-----+------+------+------+------+---------------------+ | 216 | fsx | 24 | 男 | NULL | NULL | NULL | 2018-05-19 00:12:30 | | 5 | fmp | 21 | 男 | 7 | 1 | 1 | 2018-05-18 23:00:12 | | 2 | wlz | 23 | 男 | 1 | 2 | 2 | 2018-05-18 23:00:12 | | 3 | mjd | 16 | 女 | 3 | 3 | 5 | 2018-05-18 23:00:12 | | 6 | umh | 21 | 女 | 9 | 4 | 3 | 2018-05-18 23:00:12 | | 22 | tzh | 27 | 男 | 3 | 5 | 4 | 2018-05-18 23:00:14 | | 10 | fzs | 20 | 男 | 7 | 6 | 4 | 2018-05-18 23:00:13 | | 1 | gtm | 23 | 女 | 8 | 7 | 2 | 2018-05-18 23:00:12 | | 13 | tzi | 20 | 男 | 6 | 8 | 2 | 2018-05-18 23:00:13 | | 15 | dnm | 19 | 男 | 5 | 9 | 2 | 2018-05-18 23:00:13 | | 4 | ncq | 23 | 男 | 9 | 10 | 3 | 2018-05-18 23:00:12 | +-----+------+-----+------+------+------+------+---------------------+
如:想查询总共有多少男生,多少女生?
mysql> SELECT sex,COUNT('sex') FROM students GROUP BY sex; +------+--------------+ | sex | COUNT('sex') | +------+--------------+ | 女 | 107 | | 男 | 109 | +------+--------------+ 2 rows in set (0.00 sec)
HAVING过滤,只能和GROUP BY
一起使用,用于将GROUP BY
的结果再次进行过滤,其实质和WHERE
一样
如:查询女生有多少人数
mysql> SELECT sex,COUNT('sex') FROM students GROUP BY sex HAVING sex='女'; +------+--------------+ | sex | COUNT('sex') | +------+--------------+ | 女 | 107 | +------+--------------+