mysql> create database school;
Query OK, 1 row affected (0.11 sec)
创建第一个表
mysql> use school;
Database changed
mysql> create table tb_student_info(id int not null primary key auto_increment,name varchar(30),age tinyint,sex varchar(6),height int,course_id int);
Query OK, 0 rows affected (0.19 sec)
mysql> insert into tb_students_info(name,age,sex,height,course_id) values('danny',25,'M',160,1),('green',23,'M',158,2),('henry',23,'F',185,1),('jane',23,'M',162,3),('jim',22,'F',175,2),('john',21,'F',172,4),('lily',22,'M',165,4),('susan',23,'M',170,5),('thomas',22,'F',178,5),('tom',23,'F',165,5);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql>
第二个表
mysql> create table tb_coursse(id int not null primary key auto_increment,course_name varchar(30));
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> insert into tb_coursse(course_name) values('java'),('mysql'),('python'),('go'),('c++');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
A = {1,2}
B = {3,4,5}
A×B={(1,3), (1,4), (1,5), (2,3), (2,4), (2,5) };
B×A={(3,1), (3,2), (4,1), (4,2), (5,1), (5,2) };
A×B 和 B×A 的结果就叫做两个集合的笛卡尔积。
两个集合相乘,不满足交换率,即 A×B≠B×A。
A 集合和 B 集合的笛卡尔积是 A 集合的元素个数 × B 集合的元素个数。
多表查询遵循的算法就是以上提到的笛卡尔积,表与表之间的连接可以看成是在做乘法运算
SELECT <字段名> FROM <表1> CROSS JOIN <表2> [WHERE子句];
SELECT <字段名> FROM <表1>, <表2> [WHERE子句];
字段名:需要查询的字段名称。
<表1><表2>:需要交叉连接的表名。
WHERE 子句:用来设置交叉连接的查询条件。
注意:多个表交叉连接时,在 FROM 后连续使用 CROSS JOIN 或,即可。以上两种语法的返回结果是相同的,但是第一种语法才是官方建议的标准写法。
1)查询 tb_students_info 表中的数据,SQL 语句和运行结果如下:
mysql> select * from tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | danny | 25 | M | 160 | 1 |
| 2 | green | 23 | M | 158 | 2 |
| 3 | henry | 23 | F | 185 | 1 |
| 4 | jane | 23 | M | 162 | 3 |
| 5 | jim | 22 | F | 175 | 2 |
| 6 | john | 21 | F | 172 | 4 |
| 7 | lily | 22 | M | 165 | 4 |
| 8 | susan | 23 | M | 170 | 5 |
| 9 | thomas | 22 | F | 178 | 5 |
| 10 | tom | 23 | F | 165 | 5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)
mysql> select * from tb_coursse;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | java |
| 2 | mysql |
| 3 | python |
| 4 | go |
| 5 | c++ |
+----+-------------+
5 rows in set (0.00 sec)
mysql>
mysql> select * from tb_coursse cross join tb_students_info;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | java | 1 | danny | 25 | M | 160 | 1 |
| 2 | mysql | 1 | danny | 25 | M | 160 | 1 |
| 3 | python | 1 | danny | 25 | M | 160 | 1 |
| 4 | go | 1 | danny | 25 | M | 160 | 1 |
| 5 | c++ | 1 | danny | 25 | M | 160 | 1 |
| 1 | java | 2 | green | 23 | M | 158 | 2 |
| 2 | mysql | 2 | green | 23 | M | 158 | 2 |
| 3 | python | 2 | green | 23 | M | 158 | 2 |
| 4 | go | 2 | green | 23 | M | 158 | 2 |
| 5 | c++ | 2 | green | 23 | M | 158 | 2 |
| 1 | java | 3 | henry | 23 | F | 185 | 1 |
| 2 | mysql | 3 | henry | 23 | F | 185 | 1 |
| 3 | python | 3 | henry | 23 | F | 185 | 1 |
| 4 | go | 3 | henry | 23 | F | 185 | 1 |
| 5 | c++ | 3 | henry | 23 | F | 185 | 1 |
| 1 | java | 4 | jane | 23 | M | 162 | 3 |
| 2 | mysql | 4 | jane | 23 | M | 162 | 3 |
| 3 | python | 4 | jane | 23 | M | 162 | 3 |
| 4 | go | 4 | jane | 23 | M | 162 | 3 |
| 5 | c++ | 4 | jane | 23 | M | 162 | 3 |
| 1 | java | 5 | jim | 22 | F | 175 | 2 |
| 2 | mysql | 5 | jim | 22 | F | 175 | 2 |
| 3 | python | 5 | jim | 22 | F | 175 | 2 |
| 4 | go | 5 | jim | 22 | F | 175 | 2 |
| 5 | c++ | 5 | jim | 22 | F | 175 | 2 |
| 1 | java | 6 | john | 21 | F | 172 | 4 |
| 2 | mysql | 6 | john | 21 | F | 172 | 4 |
| 3 | python | 6 | john | 21 | F | 172 | 4 |
| 4 | go | 6 | john | 21 | F | 172 | 4 |
| 5 | c++ | 6 | john | 21 | F | 172 | 4 |
| 1 | java | 7 | lily | 22 | M | 165 | 4 |
| 2 | mysql | 7 | lily | 22 | M | 165 | 4 |
| 3 | python | 7 | lily | 22 | M | 165 | 4 |
| 4 | go | 7 | lily | 22 | M | 165 | 4 |
| 5 | c++ | 7 | lily | 22 | M | 165 | 4 |
| 1 | java | 8 | susan | 23 | M | 170 | 5 |
| 2 | mysql | 8 | susan | 23 | M | 170 | 5 |
| 3 | python | 8 | susan | 23 | M | 170 | 5 |
| 4 | go | 8 | susan | 23 | M | 170 | 5 |
| 5 | c++ | 8 | susan | 23 | M | 170 | 5 |
| 1 | java | 9 | thomas | 22 | F | 178 | 5 |
| 2 | mysql | 9 | thomas | 22 | F | 178 | 5 |
| 3 | python | 9 | thomas | 22 | F | 178 | 5 |
| 4 | go | 9 | thomas | 22 | F | 178 | 5 |
| 5 | c++ | 9 | thomas | 22 | F | 178 | 5 |
| 1 | java | 10 | tom | 23 | F | 165 | 5 |
| 2 | mysql | 10 | tom | 23 | F | 165 | 5 |
| 3 | python | 10 | tom | 23 | F | 165 | 5 |
| 4 | go | 10 | tom | 23 | F | 165 | 5 |
| 5 | c++ | 10 | tom | 23 | F | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
50 rows in set (0.00 sec)
mysql> select * from tb_coursse cross join tb_students_info where tb_coursse.id = tb_students_info.course_id;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | java | 1 | danny | 25 | M | 160 | 1 |
| 2 | mysql | 2 | green | 23 | M | 158 | 2 |
| 1 | java | 3 | henry | 23 | F | 185 | 1 |
| 3 | python | 4 | jane | 23 | M | 162 | 3 |
| 2 | mysql | 5 | jim | 22 | F | 175 | 2 |
| 4 | go | 6 | john | 21 | F | 172 | 4 |
| 4 | go | 7 | lily | 22 | M | 165 | 4 |
| 5 | c++ | 8 | susan | 23 | M | 170 | 5 |
| 5 | c++ | 9 | thomas | 22 | F | 178 | 5 |
| 5 | c++ | 10 | tom | 23 | F | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)
mysql>
1.查找所有学Java的学生信息
mysql> select * from tb_coursse cross join tb_students_info where tb_coursse.id = tb_students_info.course_id and tb_coursse.course_name =
'java';
+----+-------------+----+-------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+-------+------+------+--------+-----------+
| 1 | java | 1 | danny | 25 | M | 160 | 1 |
| 1 | java | 3 | henry | 23 | F | 185 | 1 |
+----+-------------+----+-------+------+------+--------+-----------+
2 rows in set (0.00 sec)
2.查找学java的学生名字
mysql> select tb_students_info.name from tb_coursse cross join tb_stu
dents_info where tb_coursse.id = tb_students_info.course_id and tb_coursse.course_name = 'java';
+-------+
| name |
+-------+
| danny |
| henry |
+-------+
2 rows in set (0.00 sec)
mysql>
3.查找学java的学生年龄
mysql> select tb_students_info.age from tb_coursse cross join tb_students_info where tb_coursse.id = tb_students_info.course_id and tb_coursse.course_name = 'java';
+------+
| age |
+------+
| 25 |
| 23 |
+------+
2 rows in set (0.00 sec)
mysql>
4. 查找学Java的平均年龄, avg计算字段值的平均值
mysql> select avg(tb_students_info.age) from tb_coursse cross join tb_students_info where tb_coursse.id = tb_students_info.course_id and tb_coursse.course_name = 'java';
+---------------------------+
| avg(tb_students_info.age) |
+---------------------------+
| 24.0000 |
+---------------------------+
1 row in set (0.00 sec)
5.avg(tb_students_info.age) age 加个age表示别名
mysql> select avg(tb_students_info.age) age from tb_coursse cross joi
n tb_students_info where tb_coursse.id = tb_students_info.course_id a
nd tb_coursse.course_name = 'java';
+---------+
| age |
+---------+
| 24.0000 |
+---------+
1 row in set (0.00 sec)
mysql>
mysql> select * from tb_students_info inner join tb_coursse;
+----+--------+------+------+--------+-----------+----+-------------+
| id | name | age | sex | height | course_id | id | course_name |
+----+--------+------+------+--------+-----------+----+-------------+
| 1 | danny | 25 | M | 160 | 1 | 1 | java |
| 1 | danny | 25 | M | 160 | 1 | 2 | mysql |
| 1 | danny | 25 | M | 160 | 1 | 3 | python |
| 1 | danny | 25 | M | 160 | 1 | 4 | go |
| 1 | danny | 25 | M | 160 | 1 | 5 | c++ |
| 2 | green | 23 | M | 158 | 2 | 1 | java |
| 2 | green | 23 | M | 158 | 2 | 2 | mysql |
| 2 | green | 23 | M | 158 | 2 | 3 | python |
| 2 | green | 23 | M | 158 | 2 | 4 | go |
| 2 | green | 23 | M | 158 | 2 | 5 | c++ |
| 3 | henry | 23 | F | 185 | 1 | 1 | java |
| 3 | henry | 23 | F | 185 | 1 | 2 | mysql |
| 3 | henry | 23 | F | 185 | 1 | 3 | python |
| 3 | henry | 23 | F | 185 | 1 | 4 | go |
| 3 | henry | 23 | F | 185 | 1 | 5 | c++ |
| 4 | jane | 23 | M | 162 | 3 | 1 | java |
| 4 | jane | 23 | M | 162 | 3 | 2 | mysql |
| 4 | jane | 23 | M | 162 | 3 | 3 | python |
| 4 | jane | 23 | M | 162 | 3 | 4 | go |
| 4 | jane | 23 | M | 162 | 3 | 5 | c++ |
| 5 | jim | 22 | F | 175 | 2 | 1 | java |
| 5 | jim | 22 | F | 175 | 2 | 2 | mysql |
| 5 | jim | 22 | F | 175 | 2 | 3 | python |
| 5 | jim | 22 | F | 175 | 2 | 4 | go |
| 5 | jim | 22 | F | 175 | 2 | 5 | c++ |
| 6 | john | 21 | F | 172 | 4 | 1 | java |
| 6 | john | 21 | F | 172 | 4 | 2 | mysql |
| 6 | john | 21 | F | 172 | 4 | 3 | python |
| 6 | john | 21 | F | 172 | 4 | 4 | go |
| 6 | john | 21 | F | 172 | 4 | 5 | c++ |
| 7 | lily | 22 | M | 165 | 4 | 1 | java |
| 7 | lily | 22 | M | 165 | 4 | 2 | mysql |
| 7 | lily | 22 | M | 165 | 4 | 3 | python |
| 7 | lily | 22 | M | 165 | 4 | 4 | go |
| 7 | lily | 22 | M | 165 | 4 | 5 | c++ |
| 8 | susan | 23 | M | 170 | 5 | 1 | java |
| 8 | susan | 23 | M | 170 | 5 | 2 | mysql |
| 8 | susan | 23 | M | 170 | 5 | 3 | python |
| 8 | susan | 23 | M | 170 | 5 | 4 | go |
| 8 | susan | 23 | M | 170 | 5 | 5 | c++ |
| 9 | thomas | 22 | F | 178 | 5 | 1 | java |
| 9 | thomas | 22 | F | 178 | 5 | 2 | mysql |
| 9 | thomas | 22 | F | 178 | 5 | 3 | python |
| 9 | thomas | 22 | F | 178 | 5 | 4 | go |
| 9 | thomas | 22 | F | 178 | 5 | 5 | c++ |
| 10 | tom | 23 | F | 165 | 5 | 1 | java |
| 10 | tom | 23 | F | 165 | 5 | 2 | mysql |
| 10 | tom | 23 | F | 165 | 5 | 3 | python |
| 10 | tom | 23 | F | 165 | 5 | 4 | go |
| 10 | tom | 23 | F | 165 | 5 | 5 | c++ |
+----+--------+------+------+--------+-----------+----+-------------+
50 rows in set (0.00 sec)
mysql>
SELECT <字段名> FROM <表1> INNER JOIN <表2> [ON子句];
1.查找s表的名字和c表的课程名字,表s连接表c,在c.id=s.course_id
mysql> select s.name,c.course_name from tb_students_info s inner join tb_coursse c on c.id = s.course_id;
+--------+-------------+
| name | course_name |
+--------+-------------+
| danny | java |
| green | mysql |
| henry | java |
| jane | python |
| jim | mysql |
| john | go |
| lily | go |
| susan | c++ |
| thomas | c++ |
| tom | c++ |
+--------+-------------+
10 rows in set (0.00 sec)
mysql>
2.group by的意思是根据by对数据按照哪个字段进行分组,或者是哪几个字段进行分组。
count(按顺序)数数
mysql> select c.course_name,count(c.course_name) from tb_students_info s inner join tb_coursse c on s.course_id = c.id group by c.course_name;
+-------------+----------------------+
| course_name | count(c.course_name) |
+-------------+----------------------+
| c++ | 3 |
| go | 2 |
| java | 2 |
| mysql | 2 |
| python | 1 |
+-------------+----------------------+
5 rows in set (0.00 sec)
mysql>
SELECT <字段名> FROM <表1> LEFT OUTER JOIN <表2> <ON子句>;
"表1"为基表,"表2"为参考表。左连接查询时,可以查询出"表1"中的所有记录和"表2"中匹配连接条件的记录。如果"表1"的某行在"表2"中没有匹配行,那么在返回结果中,"表2"的字段值均为空值(NULL)。
字段名:需要查询的字段名称。
<表1><表2>:需要左连接的表名。
LEFT OUTER JOIN:左连接中可以省略 OUTER 关键字,只使用关键字 LEFT JOIN。
ON 子句:用来设置左连接的连接条件,不能省略
插入数据
mysql> insert tb_coursse(course_name) value ('html');
Query OK, 1 row affected (0.01 sec)
mysql> select * from tb_coursse;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | java |
| 2 | mysql |
| 3 | python |
| 4 | go |
| 5 | c++ |
| 6 | html |
+----+-------------+
6 rows in set (0.01 sec)
mysql>
mysql> insert tb_students_info(name,age,sex,height,course_id) value
('liming',22,'M',180,7);
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | danny | 25 | M | 160 | 1 |
| 2 | green | 23 | M | 158 | 2 |
| 3 | henry | 23 | F | 185 | 1 |
| 4 | jane | 23 | M | 162 | 3 |
| 5 | jim | 22 | F | 175 | 2 |
| 6 | john | 21 | F | 172 | 4 |
| 7 | lily | 22 | M | 165 | 4 |
| 8 | susan | 23 | M | 170 | 5 |
| 9 | thomas | 22 | F | 178 | 5 |
| 10 | tom | 23 | F | 165 | 5 |
| 11 | liming | 22 | M | 180 | 7 |
+----+--------+------+------+--------+-----------+
11 rows in set (0.00 sec)
mysql>
mysql> select s.name,c.course_name from tb_students_info s left outer
join tb_coursse c on s.course_id = c.id;
+--------+-------------+
| name | course_name |
+--------+-------------+
| danny | java |
| green | mysql |
| henry | java |
| jane | python |
| jim | mysql |
| john | go |
| lily | go |
| susan | c++ |
| thomas | c++ |
| tom | c++ |
| liming | NULL |
+--------+-------------+
11 rows in set (0.00 sec)
mysql>
SELECT <字段名> FROM <表1> RIGHT OUTER JOIN <表2> <ON子句>;
与左连接相反,右连接以"表2"为基表,"表1"为参考表。右连接查询时,可以查询出"表2"中的所有记录和"表1"中匹配连接条件的记录。如果"表2"的某行在"表1"中没有匹配行,那么在返回结果中,"表1"的字段值均为空值(NULL)。
mysql> select s.name,c.course_name from tb_students_info s right oute
r join tb_coursse c on s.course_id = c.id;
+--------+-------------+
| name | course_name |
+--------+-------------+
| danny | java |
| green | mysql |
| henry | java |
| jane | python |
| jim | mysql |
| john | go |
| lily | go |
| susan | c++ |
| thomas | c++ |
| tom | c++ |
| NULL | html |
+--------+-------------+
11 rows in set (0.00 sec)
mysql>
名称为 HTML 的课程目前没有学生,因为对应的tb_students_info 表中并没有该学生的信息,所以该条记录只取出了 tb_course 表中相应的值,而从 tb_students_info 表中取出的值为 NULL。
多个表左/右连接时,在 ON 子句后连续使用 LEFT/RIGHT OUTER JOIN 或 LEFT/RIGHT JOIN 即可。
使用外连接查询时,一定要分清需要查询的结果,是需要显示左表的全部记录还是右表的全部记录,然后选择相应的左连接和右连接。
在 MySQL 中,GROUP BY 关键字可以根据一个或多个字段对查询结果进行分组。
使用 GROUP BY 关键字的语法格式
GROUP BY <字段名>
单独使用 GROUP BY 关键字时,查询结果会只显示每个分组的第一条记录。
根据 tb_students_info 表中的 sex 字段进行分组查询
mysql> select name,sex from tb_students_info group by sex;
+-------+------+
| name | sex |
+-------+------+
| henry | F |
| danny | M |
+-------+------+
2 rows in set (0.00 sec)
mysql>
GROUP BY 关键字可以和 GROUP_CONCAT() 函数一起使用。GROUP_CONCAT() 函数会把每个分组的字段值都显示出来
tb_students_info 表中的 sex 字段进行分组查询,使用 GROUP_CONCAT() 函数将每个分组的 name 字段的值都显示出来
mysql> select sex,group_concat(name) from tb_students_info group by sex;
+------+------------------------------------+
| sex | group_concat(name) |
+------+------------------------------------+
| F | henry,jim,john,thomas,tom |
| M | danny,green,jane,lily,susan,liming |
+------+------------------------------------+
2 rows in set (0.00 sec)
mysql>
查询结果分为两组,sex 字段值为"女"的是一组,值为"男"的是一组,且每组的学生姓名都显示出来了
tb_students_info 表中的 age 和 sex 字段进行分组查询
mysql> select age,sex,group_concat(name) from tb_students_info group by age,sex;
+------+------+--------------------+
| age | sex | group_concat(name) |
+------+------+--------------------+
| 21 | F | john |
| 22 | F | jim,thomas |
| 22 | M | lily,liming |
| 23 | F | henry,tom |
| 23 | M | green,jane,susan |
| 25 | M | danny |
+------+------+--------------------+
6 rows in set (0.00 sec)
mysql> select age,sex,group_concat(name) name from tb_students_info g
roup by age,sex;
+------+------+------------------+
| age | sex | name |
+------+------+------------------+
| 21 | F | john |
| 22 | F | jim,thomas |
| 22 | M | lily,liming |
| 23 | F | henry,tom |
| 23 | M | green,jane,susan |
| 25 | M | danny |
+------+------+------------------+
6 rows in set (0.00 sec)
mysql> select age,sex,height,group_concat(name) name from tb_students
_info group by age,sex,height;
+------+------+--------+--------+
| age | sex | height | name |
+------+------+--------+--------+
| 21 | F | 172 | john |
| 22 | F | 175 | jim |
| 22 | F | 178 | thomas |
| 22 | M | 165 | lily |
| 22 | M | 180 | liming |
| 23 | F | 165 | tom |
| 23 | F | 185 | henry |
| 23 | M | 158 | green |
| 23 | M | 162 | jane |
| 23 | M | 170 | susan |
| 25 | M | 160 | danny |
+------+------+--------+--------+
11 rows in set (0.01 sec)
mysql> select sex,count(sex) from tb_students_info group by sex;
+------+------------+
| sex | count(sex) |
+------+------------+
| F | 5 |
| M | 6 |
+------+------------+
2 rows in set (0.00 sec)
mysql>
mysql> select sex,count(1) from tb_students_info group by sex;
+------+----------+
| sex | count(1) |
+------+----------+
| F | 5 |
| M | 6 |
+------+----------+
2 rows in set (0.01 sec)
mysql> select sex,count(*) from tb_students_info group by sex;
+------+----------+
| sex | count(*) |
+------+----------+
| F | 5 |
| M | 6 |
+------+----------+
2 rows in set (0.00 sec)
mysql> select sex,group_concat(name) from tb_students_info group by sex with rollup;
+------+--------------------------------------------------------------+
| sex | group_concat(name) |
+------+--------------------------------------------------------------+
| F | henry,jim,john,thomas,tom |
| M | danny,green,jane,lily,susan,liming |
| NULL | henry,jim,john,thomas,tom,danny,green,jane,lily,susan,liming |
+------+--------------------------------------------------------------+
3 rows in set (0.00 sec)
mysql>
WHERE <表达式> <操作符> (子查询)
操作符可以是比较运算符和 IN、NOT IN、EXISTS、NOT EXISTS 等关键字。
1)IN | NOT IN 在,不在
当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE;若使用关键字 NOT,则返回值正好相反。
2)EXISTS | NOT EXISTS 存在,不存在
用于判断子查询的结果集是否为空,若子查询的结果集不为空,返回 TRUE,否则返回 FALSE;若使用关键字 NOT,则返回的值正好相反。
1.首先单独执行内查询,查询出 tb_course 表中课程为 Java 的 id
mysql> select id from tb_coursse where course_name = 'java';
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
2.然后执行外层查询,在 tb_students_info 表中查询 course_id 等于 1 的学生姓名
mysql>
mysql> select name from tb_students_info where course_id in (1);
+-------+
| name |
+-------+
| danny |
| henry |
+-------+
2 rows in set (0.00 sec)
mysql> select name from tb_students_info where course_id in (select id
from tb_coursse where course_name = 'java');
+-------+
| name |
+-------+
| danny |
| henry |
+-------+
2 rows in set (0.14 sec)
mysql>
mysql> select id from tb_coursse where course_name = 'java';
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.01 sec)
mysql> select name from tb_students_info where course_id not in (1);
+--------+
| name |
+--------+
| green |
| jane |
| jim |
| john |
| lily |
| susan |
| thomas |
| tom |
| liming |
+--------+
9 rows in set (0.00 sec)
mysql>
mysql> select name from tb_students_info where course_id not in (select
id from tb_coursse where course_name = 'java');
+--------+
| name |
+--------+
| green |
| jane |
| jim |
| john |
| lily |
| susan |
| thomas |
| tom |
| liming |
+--------+
9 rows in set (0.00 sec)
mysql>
mysql> select id from tb_coursse where course_name = 'python';
+----+
| id |
+----+
| 3 |
+----+
1 row in set (0.00 sec)
mysql> select name from tb_students_info where course_id = (3);
+------+
| name |
+------+
| jane |
+------+
1 row in set (0.01 sec)
mysql> select name from tb_students_info where course_id in (3);
+------+
| name |
+------+
| jane |
+------+
1 row in set (0.00 sec)
mysql> select name from tb_students_info where course_id <> (3);
+--------+
| name |
+--------+
| danny |
| green |
| henry |
| jim |
| john |
| lily |
| susan |
| thomas |
| tom |
| liming |
+--------+
10 rows in set (0.00 sec)
mysql>
mysql> select course_name from tb_coursse where id = 1;
+-------------+
| course_name |
+-------------+
| java |
+-------------+
1 row in set (0.00 sec)
mysql> select * from tb_students_info where exists (select course_name
from tb_coursse where id = 1);
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | danny | 25 | M | 160 | 1 |
| 2 | green | 23 | M | 158 | 2 |
| 3 | henry | 23 | F | 185 | 1 |
| 4 | jane | 23 | M | 162 | 3 |
| 5 | jim | 22 | F | 175 | 2 |
| 6 | john | 21 | F | 172 | 4 |
| 7 | lily | 22 | M | 165 | 4 |
| 8 | susan | 23 | M | 170 | 5 |
| 9 | thomas | 22 | F | 178 | 5 |
| 10 | tom | 23 | F | 165 | 5 |
| 11 | liming | 22 | M | 180 | 7 |
+----+--------+------+------+--------+-----------+
11 rows in set (0.00 sec)
mysql>
mysql> select * from tb_students_info where age > 24 and exists (select
course_name from tb_coursse where id = 1);
+----+-------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+-------+------+------+--------+-----------+
| 1 | danny | 25 | M | 160 | 1 |
+----+-------+------+------+--------+-----------+
1 row in set (0.00 sec)
mysql>
mysql> select * from tb_students_info where age < 24 and exists (select course_name from tb_coursse where id = 1);
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 2 | green | 23 | M | 158 | 2 |
| 3 | henry | 23 | F | 185 | 1 |
| 4 | jane | 23 | M | 162 | 3 |
| 5 | jim | 22 | F | 175 | 2 |
| 6 | john | 21 | F | 172 | 4 |
| 7 | lily | 22 | M | 165 | 4 |
| 8 | susan | 23 | M | 170 | 5 |
| 9 | thomas | 22 | F | 178 | 5 |
| 10 | tom | 23 | F | 165 | 5 |
| 11 | liming | 22 | M | 180 | 7 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)