Java基础入门day40

day40

DQL

分组补充

create table student(sid int,name varchar(20),sex char(6),score double,cid int
);
​
insert into student values(100, 'wukong', 'male', 99, 1);
insert into student values(101, 'wuneng', 'male', 59, 1);
insert into student values(102, 'wujing', 'male', 60, 1);
insert into student values(103, 'tangtang', 'male', 100, 1);
​
insert into student values(104, 'baoyu', 'male', 17, 2);
insert into student values(105, 'daiyu', 'female', 16, 2);
insert into student values(106, 'baichai', 'female', 16, 2);
insert into student values(107, 'xifeng', 'female', 27, 2);
​
insert into student values(108, 'liubei', 'male', 20, 3);
insert into student values(109, 'sunquan', 'male', 20, 3);
insert into student values(110, 'caocao', 'male', 20, 3);
​
insert into student values(111, 'songjiang', 'male', 20, 4);
insert into student values(112, 'likui', 'male', 22, 4);
insert into student values(113, 'sunerniang', 'female', 20, 4);
select sex, max(score) from student group by sex;           //  查询不同性别下的最高分
select name, sex, max(score) from student group by sex;     //  有些版本的mysql执行报错,有些版本不报错,但是name值对不上
​
select cid, max(score) from student group by cid;           //  根据班级来查询不同班级的最高分
select cid, name, max(score) from student group by cid;     //  有些版本的MySQL执行报错,有些不报错,但是name值对不上
​
//  查询列中的字段的选取,要么直接是聚合函数本身,要么是分组列的值,不能包含分组列之外的值
mysql> create table student(->  sid int,->  name varchar(20),->  sex char(6),->  score double,->  cid int-> );
Query OK, 0 rows affected (0.01 sec)
​
mysql> show tables;
+----------------+
| Tables_in_saas |
+----------------+
| student        |
| tb_stu         |
+----------------+
2 rows in set (0.00 sec)
​
mysql> ^C
mysql> insert into student values(100, 'wukong', 'male', 99, 1);
Query OK, 1 row affected (0.01 sec)
​
mysql> insert into student values(101, 'wuneng', 'male', 59, 1);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(102, 'wujing', 'male', 60, 1);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(103, 'tangtang', 'male', 100, 1);
Query OK, 1 row affected (0.00 sec)
​
mysql>
mysql> insert into student values(104, 'baoyu', 'male', 17, 2);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(105, 'daiyu', 'female', 16, 2);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(106, 'baichai', 'female', 16, 2);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(107, 'xifeng', 'female', 27, 2);
Query OK, 1 row affected (0.00 sec)
​
mysql>
mysql> insert into student values(108, 'liubei', 'male', 20, 3);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(109, 'sunquan', 'male', 20, 3);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(110, 'caocao', 'male', 20, 3);
Query OK, 1 row affected (0.00 sec)
​
mysql>
mysql> insert into student values(111, 'songjiang', 'male', 20, 4);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(112, 'likui', 'male', 22, 4);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into student values(113, 'sunerniang', 'female', 20, 4);
Query OK, 1 row affected (0.00 sec)
​
mysql> select * from student;
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  100 | wukong     | male   |    99 |    1 |
|  101 | wuneng     | male   |    59 |    1 |
|  102 | wujing     | male   |    60 |    1 |
|  103 | tangtang   | male   |   100 |    1 |
|  104 | baoyu      | male   |    17 |    2 |
|  105 | daiyu      | female |    16 |    2 |
|  106 | baichai    | female |    16 |    2 |
|  107 | xifeng     | female |    27 |    2 |
|  108 | liubei     | male   |    20 |    3 |
|  109 | sunquan    | male   |    20 |    3 |
|  110 | caocao     | male   |    20 |    3 |
|  111 | songjiang  | male   |    20 |    4 |
|  112 | likui      | male   |    22 |    4 |
|  113 | sunerniang | female |    20 |    4 |
+------+------------+--------+-------+------+
14 rows in set (0.00 sec)
​
mysql> select sum(score) from student;
+------------+
| sum(score) |
+------------+
|        516 |
+------------+
1 row in set (0.00 sec)
​
mysql> select avg(score) from student;
+--------------------+
| avg(score)         |
+--------------------+
| 36.857142857142854 |
+--------------------+
1 row in set (0.00 sec)
​
mysql> select max(score) from student;
+------------+
| max(score) |
+------------+
|        100 |
+------------+
1 row in set (0.00 sec)
​
mysql> select min(score) from student;
+------------+
| min(score) |
+------------+
|         16 |
+------------+
1 row in set (0.00 sec)
​
mysql> select count(score) from student;
+--------------+
| count(score) |
+--------------+
|           14 |
+--------------+
1 row in set (0.00 sec)
​
mysql> select max(score) from student group by sex;
+------------+
| max(score) |
+------------+
|         27 |
|        100 |
+------------+
2 rows in set (0.00 sec)
​
mysql> select sex, max(score) from student group by sex;
+--------+------------+
| sex    | max(score) |
+--------+------------+
| female |         27 |
| male   |        100 |
+--------+------------+
2 rows in set (0.00 sec)
​
mysql> select name, sex, max(score) from student group by sex;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'saas.student.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
mysql> select sex, max(score) from student group by sex;
+--------+------------+
| sex    | max(score) |
+--------+------------+
| female |         27 |
| male   |        100 |
+--------+------------+
2 rows in set (0.00 sec)
​
mysql> select max(score) from student group by cid;
+------------+
| max(score) |
+------------+
|        100 |
|         27 |
|         20 |
|         22 |
+------------+
4 rows in set (0.00 sec)
​
mysql> select cid, max(score) from student group by cid;
+------+------------+
| cid  | max(score) |
+------+------------+
|    1 |        100 |
|    2 |         27 |
|    3 |         20 |
|    4 |         22 |
+------+------------+
4 rows in set (0.00 sec)
​
mysql> select cid, name, max(score) from student group by cid;
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'saas.student.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

注意:分组查询中,select显示的列只能是分组依据列,或者聚合函数,不能出现其他列

分组过滤查询

语法:
select 列名 from 表名 where 条件 group by 分组列 having 过滤规则
关键字描述
having 过滤规则过滤规则定义对于分组后的数据进行过滤

统计不同班级的最高成绩:

select cid, max(score) from student group by cid;       //  查询各个班级中最高成绩
​
+------+------------+
| cid  | max(score) |
+------+------------+
|    1 |        100 |
|    2 |         27 |
|    3 |         20 |
|    4 |         22 |
+------+------------+
​
在这个结果之上查询最高成绩大于60分的结果
select cid, max(score) from student where max(score) > 60 group by cid; //报错,where不能对于聚合函数做进一步的判断
​
select cid, max(score) from student group by cid having max(score) > 60;    //  having后面可以跟过滤规则,这个过滤规则可以使用聚合函数

限定查询

select 列名 from 表名 limit 起始行,查询行数

关键字描述
limit offset_start, row_count限定查询结果的起始行和总行数
select * from student limit 0, 5;
select * from student limit 5, 5;
select * from student limit 10, 5;

注意:在分页中,起始行是变化的,但是每一页显示的条目数是固定不变的

mysql> select * from student;
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  100 | wukong     | male   |    99 |    1 |
|  101 | wuneng     | male   |    59 |    1 |
|  102 | wujing     | male   |    60 |    1 |
|  103 | tangtang   | male   |   100 |    1 |
|  104 | baoyu      | male   |    17 |    2 |
|  105 | daiyu      | female |    16 |    2 |
|  106 | baichai    | female |    16 |    2 |
|  107 | xifeng     | female |    27 |    2 |
|  108 | liubei     | male   |    20 |    3 |
|  109 | sunquan    | male   |    20 |    3 |
|  110 | caocao     | male   |    20 |    3 |
|  111 | songjiang  | male   |    20 |    4 |
|  112 | likui      | male   |    22 |    4 |
|  113 | sunerniang | female |    20 |    4 |
+------+------------+--------+-------+------+
14 rows in set (0.00 sec)
​
mysql> select * from student limit 0, 5;
+------+----------+------+-------+------+
| sid  | name     | sex  | score | cid  |
+------+----------+------+-------+------+
|  100 | wukong   | male |    99 |    1 |
|  101 | wuneng   | male |    59 |    1 |
|  102 | wujing   | male |    60 |    1 |
|  103 | tangtang | male |   100 |    1 |
|  104 | baoyu    | male |    17 |    2 |
+------+----------+------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from student limit 5, 5;
+------+---------+--------+-------+------+
| sid  | name    | sex    | score | cid  |
+------+---------+--------+-------+------+
|  105 | daiyu   | female |    16 |    2 |
|  106 | baichai | female |    16 |    2 |
|  107 | xifeng  | female |    27 |    2 |
|  108 | liubei  | male   |    20 |    3 |
|  109 | sunquan | male   |    20 |    3 |
+------+---------+--------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from student limit 10, 5;
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  110 | caocao     | male   |    20 |    3 |
|  111 | songjiang  | male   |    20 |    4 |
|  112 | likui      | male   |    22 |    4 |
|  113 | sunerniang | female |    20 |    4 |
+------+------------+--------+-------+------+
4 rows in set (0.00 sec)
​
mysql> select * from student limit 3, 5;
+------+----------+--------+-------+------+
| sid  | name     | sex    | score | cid  |
+------+----------+--------+-------+------+
|  103 | tangtang | male   |   100 |    1 |
|  104 | baoyu    | male   |    17 |    2 |
|  105 | daiyu    | female |    16 |    2 |
|  106 | baichai  | female |    16 |    2 |
|  107 | xifeng   | female |    27 |    2 |
+------+----------+--------+-------+------+
5 rows in set (0.00 sec)
​
mysql> select * from student limit 0, 6;
+------+----------+--------+-------+------+
| sid  | name     | sex    | score | cid  |
+------+----------+--------+-------+------+
|  100 | wukong   | male   |    99 |    1 |
|  101 | wuneng   | male   |    59 |    1 |
|  102 | wujing   | male   |    60 |    1 |
|  103 | tangtang | male   |   100 |    1 |
|  104 | baoyu    | male   |    17 |    2 |
|  105 | daiyu    | female |    16 |    2 |
+------+----------+--------+-------+------+
6 rows in set (0.00 sec)
​
mysql> select * from student limit 6, 6;
+------+-----------+--------+-------+------+
| sid  | name      | sex    | score | cid  |
+------+-----------+--------+-------+------+
|  106 | baichai   | female |    16 |    2 |
|  107 | xifeng    | female |    27 |    2 |
|  108 | liubei    | male   |    20 |    3 |
|  109 | sunquan   | male   |    20 |    3 |
|  110 | caocao    | male   |    20 |    3 |
|  111 | songjiang | male   |    20 |    4 |
+------+-----------+--------+-------+------+
6 rows in set (0.00 sec)
​
mysql> select * from student limit 12, 6;
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  112 | likui      | male   |    22 |    4 |
|  113 | sunerniang | female |    20 |    4 |
+------+------------+--------+-------+------+
2 rows in set (0.00 sec)

查询总结

完整的SQL语句编写顺序:

select 列名 from 表名 where 条件 group by 分组 having 过滤条件 order by 排序列 [asc|desc] limit 起始行,总条目数

执行顺序

  1. from: 指定数据来源表

  2. where:对于查询数据做第一次过滤

  3. group by: 分组

  4. having:对于分组后的数据进行二次过滤

  5. select:查询各个字段的值

  6. order by:排序

  7. limit: 限定查询结果

子查询

将一个查询结果作为一条条件判断

select 列名 from 表名 where 条件(子查询结果)

查询比baoyu成绩更高的所有同学

mysql> select score from student where name = 'baoyu';
+-------+
| score |
+-------+
|    17 |
+-------+
1 row in set (0.00 sec)mysql> select * from student where score > 17;
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  100 | wukong     | male   |    99 |    1 |
|  101 | wuneng     | male   |    59 |    1 |
|  102 | wujing     | male   |    60 |    1 |
|  103 | tangtang   | male   |   100 |    1 |
|  107 | xifeng     | female |    27 |    2 |
|  108 | liubei     | male   |    20 |    3 |
|  109 | sunquan    | male   |    20 |    3 |
|  110 | caocao     | male   |    20 |    3 |
|  111 | songjiang  | male   |    20 |    4 |
|  112 | likui      | male   |    22 |    4 |
|  113 | sunerniang | female |    20 |    4 |
+------+------------+--------+-------+------+
11 rows in set (0.00 sec)

现在用子查询将两条SQL语句合并

mysql> select * from student where score > (select score from student where name = 'baoyu');
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  100 | wukong     | male   |    99 |    1 |
|  101 | wuneng     | male   |    59 |    1 |
|  102 | wujing     | male   |    60 |    1 |
|  103 | tangtang   | male   |   100 |    1 |
|  107 | xifeng     | female |    27 |    2 |
|  108 | liubei     | male   |    20 |    3 |
|  109 | sunquan    | male   |    20 |    3 |
|  110 | caocao     | male   |    20 |    3 |
|  111 | songjiang  | male   |    20 |    4 |
|  112 | likui      | male   |    22 |    4 |
|  113 | sunerniang | female |    20 |    4 |
+------+------------+--------+-------+------+
11 rows in set (0.01 sec)

在查询中嵌套查询,内部的查询就被称之为子查询

将子查询的“一行一列”的结果作为外部条件,进行二次查询

子查询得到一行一列的结果才能作为外部查询的等值判断或者不等值条件判断

将查询结果作为枚举查询

mysql> select * from student;
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  100 | wukong     | male   |    99 |    1 |
|  101 | wuneng     | male   |    59 |    1 |
|  102 | wujing     | male   |    60 |    1 |
|  103 | tangtang   | male   |   100 |    1 |
|  104 | baoyu      | male   |    17 |    2 |
|  105 | daiyu      | female |    16 |    2 |
|  106 | baichai    | female |    16 |    2 |
|  107 | xifeng     | female |    27 |    2 |
|  108 | liubei     | male   |    20 |    3 |
|  109 | sunquan    | male   |    20 |    3 |
|  110 | caocao     | male   |    20 |    3 |
|  111 | songjiang  | male   |    20 |    4 |
|  112 | likui      | male   |    22 |    4 |
|  113 | sunerniang | female |    20 |    4 |
+------+------------+--------+-------+------+
14 rows in set (0.00 sec)mysql> select cid from student where name = 'baoyu';
+------+
| cid  |
+------+
|    2 |
+------+
1 row in set (0.00 sec)
mysql> select * from student where cid in (2);
+------+---------+--------+-------+------+
| sid  | name    | sex    | score | cid  |
+------+---------+--------+-------+------+
|  104 | baoyu   | male   |    17 |    2 |
|  105 | daiyu   | female |    16 |    2 |
|  106 | baichai | female |    16 |    2 |
|  107 | xifeng  | female |    27 |    2 |
+------+---------+--------+-------+------+
4 rows in set (0.01 sec)mysql> select cid from student;
+------+
| cid  |
+------+
|    1 |
|    1 |
|    1 |
|    1 |
|    2 |
|    2 |
|    2 |
|    2 |
|    3 |
|    3 |
|    3 |
|    4 |
|    4 |
|    4 |
+------+
14 rows in set (0.00 sec)mysql> select distinct cid from student;
+------+
| cid  |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)mysql> select * from student where cid in (select distinct cid from student);
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  100 | wukong     | male   |    99 |    1 |
|  101 | wuneng     | male   |    59 |    1 |
|  102 | wujing     | male   |    60 |    1 |
|  103 | tangtang   | male   |   100 |    1 |
|  104 | baoyu      | male   |    17 |    2 |
|  105 | daiyu      | female |    16 |    2 |
|  106 | baichai    | female |    16 |    2 |
|  107 | xifeng     | female |    27 |    2 |
|  108 | liubei     | male   |    20 |    3 |
|  109 | sunquan    | male   |    20 |    3 |
|  110 | caocao     | male   |    20 |    3 |
|  111 | songjiang  | male   |    20 |    4 |
|  112 | likui      | male   |    22 |    4 |
|  113 | sunerniang | female |    20 |    4 |
+------+------------+--------+-------+------+
14 rows in set (0.00 sec)

将子查询的“多行一列”的结果作为外部查询的枚举查询条件,做二次查询

案例:

查询比4班同学成绩高的同学

mysql> select score from student where cid = 4;
+-------+
| score |
+-------+
|    20 |
|    22 |
|    20 |
+-------+
3 rows in set (0.00 sec)mysql> select * from student where score > all(select score from student where cid = 4);
+------+----------+--------+-------+------+
| sid  | name     | sex    | score | cid  |
+------+----------+--------+-------+------+
|  100 | wukong   | male   |    99 |    1 |
|  101 | wuneng   | male   |    59 |    1 |
|  102 | wujing   | male   |    60 |    1 |
|  103 | tangtang | male   |   100 |    1 |
|  107 | xifeng   | female |    27 |    2 |
+------+----------+--------+-------+------+
5 rows in set (0.00 sec)mysql> select * from student where score > any(select score from student where cid = 4);
+------+----------+--------+-------+------+
| sid  | name     | sex    | score | cid  |
+------+----------+--------+-------+------+
|  100 | wukong   | male   |    99 |    1 |
|  101 | wuneng   | male   |    59 |    1 |
|  102 | wujing   | male   |    60 |    1 |
|  103 | tangtang | male   |   100 |    1 |
|  107 | xifeng   | female |    27 |    2 |
|  112 | likui    | male   |    22 |    4 |
+------+----------+--------+-------+------+
6 rows in set (0.00 sec)

注意:当子查询结果集形式为多行单列时可以使用any或者all关键字

将子查询作为一张表

select 列名 from 表名 from(子查询结果) where 条件

案例

查询成绩排名前五的所有同学

先查出来所有的同学的信息,按照成绩从高到底的排列顺序

mysql> select * from student order by score desc;
+------+------------+--------+-------+------+
| sid  | name       | sex    | score | cid  |
+------+------------+--------+-------+------+
|  103 | tangtang   | male   |   100 |    1 |
|  100 | wukong     | male   |    99 |    1 |
|  102 | wujing     | male   |    60 |    1 |
|  101 | wuneng     | male   |    59 |    1 |
|  107 | xifeng     | female |    27 |    2 |
|  112 | likui      | male   |    22 |    4 |
|  108 | liubei     | male   |    20 |    3 |
|  109 | sunquan    | male   |    20 |    3 |
|  110 | caocao     | male   |    20 |    3 |
|  111 | songjiang  | male   |    20 |    4 |
|  113 | sunerniang | female |    20 |    4 |
|  104 | baoyu      | male   |    17 |    2 |
|  105 | daiyu      | female |    16 |    2 |
|  106 | baichai    | female |    16 |    2 |
+------+------------+--------+-------+------+
14 rows in set (0.00 sec)
mysql> select * from (select * from student order by score desc) orderScore limit 0, 5;
+------+----------+--------+-------+------+
| sid  | name     | sex    | score | cid  |
+------+----------+--------+-------+------+
|  103 | tangtang | male   |   100 |    1 |
|  100 | wukong   | male   |    99 |    1 |
|  102 | wujing   | male   |    60 |    1 |
|  101 | wuneng   | male   |    59 |    1 |
|  107 | xifeng   | female |    27 |    2 |
+------+----------+--------+-------+------+
5 rows in set (0.00 sec)

注意:将子查询的“多行多列”的结果作为外查询的一张虚拟表,做二次查询

子查询作为临时表,要为其赋予一个临时表表名

合并查询

语法:

select * from 表1 union select * from 表2
select * from 表1 union all select * from 表2

合并两张表的结果

注意:合并结果的两张表,列数必须相同,列的数据类型可以不同

mysql> select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)mysql> select * from student;
+------+------------+--------+-------+------+------+
| sid  | name       | sex    | score | cid  | age  |
+------+------------+--------+-------+------+------+
|  100 | wukong     | male   |    99 |    1 | NULL |
|  101 | wuneng     | male   |    59 |    1 | NULL |
|  102 | wujing     | male   |    60 |    1 | NULL |
|  103 | tangtang   | male   |   100 |    1 | NULL |
|  104 | baoyu      | male   |    17 |    2 | NULL |
|  105 | daiyu      | female |    16 |    2 | NULL |
|  106 | baichai    | female |    16 |    2 | NULL |
|  107 | xifeng     | female |    27 |    2 | NULL |
|  108 | liubei     | male   |    20 |    3 | NULL |
|  109 | sunquan    | male   |    20 |    3 | NULL |
|  110 | caocao     | male   |    20 |    3 | NULL |
|  111 | songjiang  | male   |    20 |    4 | NULL |
|  112 | likui      | male   |    22 |    4 | NULL |
|  113 | sunerniang | female |    20 |    4 | NULL |
+------+------------+--------+-------+------+------+
14 rows in set (0.00 sec)mysql> select * from tb_stu union select * from student;
+------+--------------+--------+------+-------+------+
| sid  | name         | sex    | tel  | score | age  |
+------+--------------+--------+------+-------+------+
| 9527 | zhouxingxing | boy    | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl   | 110  |    80 |   18 |
| 9529 | shiliu       | girl   | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl   | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy    | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL   | NULL |  NULL | NULL |
|  100 | wukong       | male   | 99   |     1 | NULL |
|  101 | wuneng       | male   | 59   |     1 | NULL |
|  102 | wujing       | male   | 60   |     1 | NULL |
|  103 | tangtang     | male   | 100  |     1 | NULL |
|  104 | baoyu        | male   | 17   |     2 | NULL |
|  105 | daiyu        | female | 16   |     2 | NULL |
|  106 | baichai      | female | 16   |     2 | NULL |
|  107 | xifeng       | female | 27   |     2 | NULL |
|  108 | liubei       | male   | 20   |     3 | NULL |
|  109 | sunquan      | male   | 20   |     3 | NULL |
|  110 | caocao       | male   | 20   |     3 | NULL |
|  111 | songjiang    | male   | 20   |     4 | NULL |
|  112 | likui        | male   | 22   |     4 | NULL |
|  113 | sunerniang   | female | 20   |     4 | NULL |
+------+--------------+--------+------+-------+------+
20 rows in set (0.00 sec)mysql> desc tb_stu;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| sex   | char(4)     | YES  |     | NULL    |       |
| tel   | char(11)    | YES  |     | NULL    |       |
| score | double      | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| sex   | char(6)     | YES  |     | NULL    |       |
| score | double      | YES  |     | NULL    |       |
| cid   | int(11)     | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)mysql> select * from student union select * from tb_stu;
+------+--------------+--------+-------+------+------+
| sid  | name         | sex    | score | cid  | age  |
+------+--------------+--------+-------+------+------+
|  100 | wukong       | male   | 99    |    1 | NULL |
|  101 | wuneng       | male   | 59    |    1 | NULL |
|  102 | wujing       | male   | 60    |    1 | NULL |
|  103 | tangtang     | male   | 100   |    1 | NULL |
|  104 | baoyu        | male   | 17    |    2 | NULL |
|  105 | daiyu        | female | 16    |    2 | NULL |
|  106 | baichai      | female | 16    |    2 | NULL |
|  107 | xifeng       | female | 27    |    2 | NULL |
|  108 | liubei       | male   | 20    |    3 | NULL |
|  109 | sunquan      | male   | 20    |    3 | NULL |
|  110 | caocao       | male   | 20    |    3 | NULL |
|  111 | songjiang    | male   | 20    |    4 | NULL |
|  112 | likui        | male   | 22    |    4 | NULL |
|  113 | sunerniang   | female | 20    |    4 | NULL |
| 9527 | zhouxingxing | boy    | 119   |   99 |   20 |
| 9528 | qiuxiang     | girl   | 110   |   80 |   18 |
| 9529 | shiliu       | girl   | 114   |   59 |   30 |
| 9530 | qiuxiang     | girl   | 110   |   80 |   18 |
| 9531 | zhuzhishan   | boy    | 112   |   77 |   22 |
| 9532 | chunxiang    | NULL   | NULL  | NULL | NULL |
+------+--------------+--------+-------+------+------+
20 rows in set (0.00 sec)mysql> select * from student union select * from student;
+------+------------+--------+-------+------+------+
| sid  | name       | sex    | score | cid  | age  |
+------+------------+--------+-------+------+------+
|  100 | wukong     | male   |    99 |    1 | NULL |
|  101 | wuneng     | male   |    59 |    1 | NULL |
|  102 | wujing     | male   |    60 |    1 | NULL |
|  103 | tangtang   | male   |   100 |    1 | NULL |
|  104 | baoyu      | male   |    17 |    2 | NULL |
|  105 | daiyu      | female |    16 |    2 | NULL |
|  106 | baichai    | female |    16 |    2 | NULL |
|  107 | xifeng     | female |    27 |    2 | NULL |
|  108 | liubei     | male   |    20 |    3 | NULL |
|  109 | sunquan    | male   |    20 |    3 | NULL |
|  110 | caocao     | male   |    20 |    3 | NULL |
|  111 | songjiang  | male   |    20 |    4 | NULL |
|  112 | likui      | male   |    22 |    4 | NULL |
|  113 | sunerniang | female |    20 |    4 | NULL |
+------+------------+--------+-------+------+------+
14 rows in set (0.00 sec)mysql> select * from student union all select * from student;
+------+------------+--------+-------+------+------+
| sid  | name       | sex    | score | cid  | age  |
+------+------------+--------+-------+------+------+
|  100 | wukong     | male   |    99 |    1 | NULL |
|  101 | wuneng     | male   |    59 |    1 | NULL |
|  102 | wujing     | male   |    60 |    1 | NULL |
|  103 | tangtang   | male   |   100 |    1 | NULL |
|  104 | baoyu      | male   |    17 |    2 | NULL |
|  105 | daiyu      | female |    16 |    2 | NULL |
|  106 | baichai    | female |    16 |    2 | NULL |
|  107 | xifeng     | female |    27 |    2 | NULL |
|  108 | liubei     | male   |    20 |    3 | NULL |
|  109 | sunquan    | male   |    20 |    3 | NULL |
|  110 | caocao     | male   |    20 |    3 | NULL |
|  111 | songjiang  | male   |    20 |    4 | NULL |
|  112 | likui      | male   |    22 |    4 | NULL |
|  113 | sunerniang | female |    20 |    4 | NULL |
|  100 | wukong     | male   |    99 |    1 | NULL |
|  101 | wuneng     | male   |    59 |    1 | NULL |
|  102 | wujing     | male   |    60 |    1 | NULL |
|  103 | tangtang   | male   |   100 |    1 | NULL |
|  104 | baoyu      | male   |    17 |    2 | NULL |
|  105 | daiyu      | female |    16 |    2 | NULL |
|  106 | baichai    | female |    16 |    2 | NULL |
|  107 | xifeng     | female |    27 |    2 | NULL |
|  108 | liubei     | male   |    20 |    3 | NULL |
|  109 | sunquan    | male   |    20 |    3 | NULL |
|  110 | caocao     | male   |    20 |    3 | NULL |
|  111 | songjiang  | male   |    20 |    4 | NULL |
|  112 | likui      | male   |    22 |    4 | NULL |
|  113 | sunerniang | female |    20 |    4 | NULL |
+------+------------+--------+-------+------+------+
28 rows in set (0.00 sec)mysql> select * from tb_stu union all select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
12 rows in set (0.00 sec)mysql> select * from tb_stu union select * from tb_stu;
+------+--------------+------+------+-------+------+
| sid  | name         | sex  | tel  | score | age  |
+------+--------------+------+------+-------+------+
| 9527 | zhouxingxing | boy  | 119  |    99 |   20 |
| 9528 | qiuxiang     | girl | 110  |    80 |   18 |
| 9529 | shiliu       | girl | 114  |    59 |   30 |
| 9530 | qiuxiang     | girl | 110  |    80 |   18 |
| 9531 | zhuzhishan   | boy  | 112  |    77 |   22 |
| 9532 | chunxiang    | NULL | NULL |  NULL | NULL |
+------+--------------+------+------+-------+------+
6 rows in set (0.00 sec)

经验:union合并结果集,会去除掉两张表中重复的数据

表连接

准备:

create table clazz(cid int,cname varchar(20),cdesc varchar(20)
);insert into clazz values(1, 'computer', 'computer desc');
insert into clazz values(2, 'software', 'software desc');
insert into clazz values(3, 'math', 'math desc');
insert into clazz values(4, 'biological', 'biological desc');
insert into clazz values(5, 'education', 'education desc');

语法:

select 列名 from 表1 连接方式 表2 on 连接条件

内连接

select * from student s inner join clazz c on s.cid = c.cid
mysql> select * from student s inner join clazz c on s.cid = c.cid;
+------+------------+--------+-------+------+------+------+------------+-----------------+
| sid  | name       | sex    | score | cid  | age  | cid  | cname      | cdesc           |
+------+------------+--------+-------+------+------+------+------------+-----------------+
|  100 | wukong     | male   |    99 |    1 | NULL |    1 | computer   | computer desc   |
|  101 | wuneng     | male   |    59 |    1 | NULL |    1 | computer   | computer desc   |
|  102 | wujing     | male   |    60 |    1 | NULL |    1 | computer   | computer desc   |
|  103 | tangtang   | male   |   100 |    1 | NULL |    1 | computer   | computer desc   |
|  104 | baoyu      | male   |    17 |    2 | NULL |    2 | software   | software desc   |
|  105 | daiyu      | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  106 | baichai    | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  107 | xifeng     | female |    27 |    2 | NULL |    2 | software   | software desc   |
|  108 | liubei     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  109 | sunquan    | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  110 | caocao     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  111 | songjiang  | male   |    20 |    4 | NULL |    4 | biological | biological desc |
|  112 | likui      | male   |    22 |    4 | NULL |    4 | biological | biological desc |
|  113 | sunerniang | female |    20 |    4 | NULL |    4 | biological | biological desc |
+------+------------+--------+-------+------+------+------+------------+-----------------+
14 rows in set (0.00 sec)

左外连接

select * from student s left outer join clazz c on s.cid = c.cid
mysql> select * from student s left outer join clazz c on s.cid = c.cid;
+------+--------------+--------+-------+------+------+------+------------+-----------------+
| sid  | name         | sex    | score | cid  | age  | cid  | cname      | cdesc           |
+------+--------------+--------+-------+------+------+------+------------+-----------------+
|  100 | wukong       | male   |    99 |    1 | NULL |    1 | computer   | computer desc   |
|  101 | wuneng       | male   |    59 |    1 | NULL |    1 | computer   | computer desc   |
|  102 | wujing       | male   |    60 |    1 | NULL |    1 | computer   | computer desc   |
|  103 | tangtang     | male   |   100 |    1 | NULL |    1 | computer   | computer desc   |
|  104 | baoyu        | male   |    17 |    2 | NULL |    2 | software   | software desc   |
|  105 | daiyu        | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  106 | baichai      | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  107 | xifeng       | female |    27 |    2 | NULL |    2 | software   | software desc   |
|  108 | liubei       | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  109 | sunquan      | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  110 | caocao       | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  111 | songjiang    | male   |    20 |    4 | NULL |    4 | biological | biological desc |
|  112 | likui        | male   |    22 |    4 | NULL |    4 | biological | biological desc |
|  113 | sunerniang   | female |    20 |    4 | NULL |    4 | biological | biological desc |
|  114 | zhouhuaqiang | male   |    50 |    9 |   30 | NULL | NULL       | NULL            |
+------+--------------+--------+-------+------+------+------+------------+-----------------+
15 rows in set (0.00 sec)mysql> select * from student s left join clazz c on s.cid = c.cid;
+------+--------------+--------+-------+------+------+------+------------+-----------------+
| sid  | name         | sex    | score | cid  | age  | cid  | cname      | cdesc           |
+------+--------------+--------+-------+------+------+------+------------+-----------------+
|  100 | wukong       | male   |    99 |    1 | NULL |    1 | computer   | computer desc   |
|  101 | wuneng       | male   |    59 |    1 | NULL |    1 | computer   | computer desc   |
|  102 | wujing       | male   |    60 |    1 | NULL |    1 | computer   | computer desc   |
|  103 | tangtang     | male   |   100 |    1 | NULL |    1 | computer   | computer desc   |
|  104 | baoyu        | male   |    17 |    2 | NULL |    2 | software   | software desc   |
|  105 | daiyu        | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  106 | baichai      | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  107 | xifeng       | female |    27 |    2 | NULL |    2 | software   | software desc   |
|  108 | liubei       | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  109 | sunquan      | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  110 | caocao       | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  111 | songjiang    | male   |    20 |    4 | NULL |    4 | biological | biological desc |
|  112 | likui        | male   |    22 |    4 | NULL |    4 | biological | biological desc |
|  113 | sunerniang   | female |    20 |    4 | NULL |    4 | biological | biological desc |
|  114 | zhouhuaqiang | male   |    50 |    9 |   30 | NULL | NULL       | NULL            |
+------+--------------+--------+-------+------+------+------+------------+-----------------+
15 rows in set (0.00 sec)	

左外连接left outer join,也可以简写为left join

以左表为基准,查找右表,找到了则显示右表,没找到右表里面的所有信息为null

mysql> select * from student s right outer join clazz c on s.cid = c.cid;
+------+------------+--------+-------+------+------+------+------------+-----------------+
| sid  | name       | sex    | score | cid  | age  | cid  | cname      | cdesc           |
+------+------------+--------+-------+------+------+------+------------+-----------------+
|  100 | wukong     | male   |    99 |    1 | NULL |    1 | computer   | computer desc   |
|  101 | wuneng     | male   |    59 |    1 | NULL |    1 | computer   | computer desc   |
|  102 | wujing     | male   |    60 |    1 | NULL |    1 | computer   | computer desc   |
|  103 | tangtang   | male   |   100 |    1 | NULL |    1 | computer   | computer desc   |
|  104 | baoyu      | male   |    17 |    2 | NULL |    2 | software   | software desc   |
|  105 | daiyu      | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  106 | baichai    | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  107 | xifeng     | female |    27 |    2 | NULL |    2 | software   | software desc   |
|  108 | liubei     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  109 | sunquan    | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  110 | caocao     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  111 | songjiang  | male   |    20 |    4 | NULL |    4 | biological | biological desc |
|  112 | likui      | male   |    22 |    4 | NULL |    4 | biological | biological desc |
|  113 | sunerniang | female |    20 |    4 | NULL |    4 | biological | biological desc |
| NULL | NULL       | NULL   |  NULL | NULL | NULL |    5 | education  | education desc  |
+------+------------+--------+-------+------+------+------+------------+-----------------+
15 rows in set (0.00 sec)mysql> select * from student s right join clazz c on s.cid = c.cid;
+------+------------+--------+-------+------+------+------+------------+-----------------+
| sid  | name       | sex    | score | cid  | age  | cid  | cname      | cdesc           |
+------+------------+--------+-------+------+------+------+------------+-----------------+
|  100 | wukong     | male   |    99 |    1 | NULL |    1 | computer   | computer desc   |
|  101 | wuneng     | male   |    59 |    1 | NULL |    1 | computer   | computer desc   |
|  102 | wujing     | male   |    60 |    1 | NULL |    1 | computer   | computer desc   |
|  103 | tangtang   | male   |   100 |    1 | NULL |    1 | computer   | computer desc   |
|  104 | baoyu      | male   |    17 |    2 | NULL |    2 | software   | software desc   |
|  105 | daiyu      | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  106 | baichai    | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  107 | xifeng     | female |    27 |    2 | NULL |    2 | software   | software desc   |
|  108 | liubei     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  109 | sunquan    | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  110 | caocao     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  111 | songjiang  | male   |    20 |    4 | NULL |    4 | biological | biological desc |
|  112 | likui      | male   |    22 |    4 | NULL |    4 | biological | biological desc |
|  113 | sunerniang | female |    20 |    4 | NULL |    4 | biological | biological desc |
| NULL | NULL       | NULL   |  NULL | NULL | NULL |    5 | education  | education desc  |
+------+------------+--------+-------+------+------+------+------------+-----------------+
15 rows in set (0.00 sec)

6*-右外连接right outer join, 也可以简写为right join

以右表为基准,查找坐标,找到了则显示左表,没找到左表里面的所有信息为null

笛卡尔积:

笛卡尔积是两张表所有的记录的乘积

mysql> select * from clazz;
+------+------------+-----------------+
| cid  | cname      | cdesc           |
+------+------------+-----------------+
|    1 | computer   | computer desc   |
|    2 | software   | software desc   |
|    3 | math       | math desc       |
|    4 | biological | biological desc |
|    5 | education  | education desc  |
+------+------------+-----------------+
5 rows in set (0.00 sec)
​
mysql> select * from student;
+------+------------+--------+-------+------+------+
| sid  | name       | sex    | score | cid  | age  |
+------+------------+--------+-------+------+------+
|  100 | wukong     | male   |    99 |    1 | NULL |
|  101 | wuneng     | male   |    59 |    1 | NULL |
|  102 | wujing     | male   |    60 |    1 | NULL |
|  103 | tangtang   | male   |   100 |    1 | NULL |
|  104 | baoyu      | male   |    17 |    2 | NULL |
|  105 | daiyu      | female |    16 |    2 | NULL |
|  106 | baichai    | female |    16 |    2 | NULL |
|  107 | xifeng     | female |    27 |    2 | NULL |
|  108 | liubei     | male   |    20 |    3 | NULL |
|  109 | sunquan    | male   |    20 |    3 | NULL |
|  110 | caocao     | male   |    20 |    3 | NULL |
|  111 | songjiang  | male   |    20 |    4 | NULL |
|  112 | likui      | male   |    22 |    4 | NULL |
|  113 | sunerniang | female |    20 |    4 | NULL |
+------+------------+--------+-------+------+------+
14 rows in set (0.00 sec)
mysql> select * from student s, clazz c;
+------+------------+--------+-------+------+------+------+------------+-----------------+
| sid  | name       | sex    | score | cid  | age  | cid  | cname      | cdesc           |
+------+------------+--------+-------+------+------+------+------------+-----------------+
|  100 | wukong     | male   |    99 |    1 | NULL |    1 | computer   | computer desc   |
|  100 | wukong     | male   |    99 |    1 | NULL |    2 | software   | software desc   |
|  100 | wukong     | male   |    99 |    1 | NULL |    3 | math       | math desc       |
|  100 | wukong     | male   |    99 |    1 | NULL |    4 | biological | biological desc |
|  100 | wukong     | male   |    99 |    1 | NULL |    5 | education  | education desc  |
|  101 | wuneng     | male   |    59 |    1 | NULL |    1 | computer   | computer desc   |
|  101 | wuneng     | male   |    59 |    1 | NULL |    2 | software   | software desc   |
|  101 | wuneng     | male   |    59 |    1 | NULL |    3 | math       | math desc       |
|  101 | wuneng     | male   |    59 |    1 | NULL |    4 | biological | biological desc |
|  101 | wuneng     | male   |    59 |    1 | NULL |    5 | education  | education desc  |
|  102 | wujing     | male   |    60 |    1 | NULL |    1 | computer   | computer desc   |
|  102 | wujing     | male   |    60 |    1 | NULL |    2 | software   | software desc   |
|  102 | wujing     | male   |    60 |    1 | NULL |    3 | math       | math desc       |
|  102 | wujing     | male   |    60 |    1 | NULL |    4 | biological | biological desc |
|  102 | wujing     | male   |    60 |    1 | NULL |    5 | education  | education desc  |
|  103 | tangtang   | male   |   100 |    1 | NULL |    1 | computer   | computer desc   |
|  103 | tangtang   | male   |   100 |    1 | NULL |    2 | software   | software desc   |
|  103 | tangtang   | male   |   100 |    1 | NULL |    3 | math       | math desc       |
|  103 | tangtang   | male   |   100 |    1 | NULL |    4 | biological | biological desc |
|  103 | tangtang   | male   |   100 |    1 | NULL |    5 | education  | education desc  |
|  104 | baoyu      | male   |    17 |    2 | NULL |    1 | computer   | computer desc   |
|  104 | baoyu      | male   |    17 |    2 | NULL |    2 | software   | software desc   |
|  104 | baoyu      | male   |    17 |    2 | NULL |    3 | math       | math desc       |
|  104 | baoyu      | male   |    17 |    2 | NULL |    4 | biological | biological desc |
|  104 | baoyu      | male   |    17 |    2 | NULL |    5 | education  | education desc  |
|  105 | daiyu      | female |    16 |    2 | NULL |    1 | computer   | computer desc   |
|  105 | daiyu      | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  105 | daiyu      | female |    16 |    2 | NULL |    3 | math       | math desc       |
|  105 | daiyu      | female |    16 |    2 | NULL |    4 | biological | biological desc |
|  105 | daiyu      | female |    16 |    2 | NULL |    5 | education  | education desc  |
|  106 | baichai    | female |    16 |    2 | NULL |    1 | computer   | computer desc   |
|  106 | baichai    | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  106 | baichai    | female |    16 |    2 | NULL |    3 | math       | math desc       |
|  106 | baichai    | female |    16 |    2 | NULL |    4 | biological | biological desc |
|  106 | baichai    | female |    16 |    2 | NULL |    5 | education  | education desc  |
|  107 | xifeng     | female |    27 |    2 | NULL |    1 | computer   | computer desc   |
|  107 | xifeng     | female |    27 |    2 | NULL |    2 | software   | software desc   |
|  107 | xifeng     | female |    27 |    2 | NULL |    3 | math       | math desc       |
|  107 | xifeng     | female |    27 |    2 | NULL |    4 | biological | biological desc |
|  107 | xifeng     | female |    27 |    2 | NULL |    5 | education  | education desc  |
|  108 | liubei     | male   |    20 |    3 | NULL |    1 | computer   | computer desc   |
|  108 | liubei     | male   |    20 |    3 | NULL |    2 | software   | software desc   |
|  108 | liubei     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  108 | liubei     | male   |    20 |    3 | NULL |    4 | biological | biological desc |
|  108 | liubei     | male   |    20 |    3 | NULL |    5 | education  | education desc  |
|  109 | sunquan    | male   |    20 |    3 | NULL |    1 | computer   | computer desc   |
|  109 | sunquan    | male   |    20 |    3 | NULL |    2 | software   | software desc   |
|  109 | sunquan    | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  109 | sunquan    | male   |    20 |    3 | NULL |    4 | biological | biological desc |
|  109 | sunquan    | male   |    20 |    3 | NULL |    5 | education  | education desc  |
|  110 | caocao     | male   |    20 |    3 | NULL |    1 | computer   | computer desc   |
|  110 | caocao     | male   |    20 |    3 | NULL |    2 | software   | software desc   |
|  110 | caocao     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  110 | caocao     | male   |    20 |    3 | NULL |    4 | biological | biological desc |
|  110 | caocao     | male   |    20 |    3 | NULL |    5 | education  | education desc  |
|  111 | songjiang  | male   |    20 |    4 | NULL |    1 | computer   | computer desc   |
|  111 | songjiang  | male   |    20 |    4 | NULL |    2 | software   | software desc   |
|  111 | songjiang  | male   |    20 |    4 | NULL |    3 | math       | math desc       |
|  111 | songjiang  | male   |    20 |    4 | NULL |    4 | biological | biological desc |
|  111 | songjiang  | male   |    20 |    4 | NULL |    5 | education  | education desc  |
|  112 | likui      | male   |    22 |    4 | NULL |    1 | computer   | computer desc   |
|  112 | likui      | male   |    22 |    4 | NULL |    2 | software   | software desc   |
|  112 | likui      | male   |    22 |    4 | NULL |    3 | math       | math desc       |
|  112 | likui      | male   |    22 |    4 | NULL |    4 | biological | biological desc |
|  112 | likui      | male   |    22 |    4 | NULL |    5 | education  | education desc  |
|  113 | sunerniang | female |    20 |    4 | NULL |    1 | computer   | computer desc   |
|  113 | sunerniang | female |    20 |    4 | NULL |    2 | software   | software desc   |
|  113 | sunerniang | female |    20 |    4 | NULL |    3 | math       | math desc       |
|  113 | sunerniang | female |    20 |    4 | NULL |    4 | biological | biological desc |
|  113 | sunerniang | female |    20 |    4 | NULL |    5 | education  | education desc  |
+------+------------+--------+-------+------+------+------+------------+-----------------+
70 rows in set (0.00 sec)

第一张表14条记录,第二表5条记录,最终笛卡尔积的总条目是14*5 = 70.

通过运行结果我们发现,笛卡尔积是第一张表的每一条记录都与第二张表分别关联

等值查询

mysql> select * from student;
+------+--------------+--------+-------+------+------+
| sid  | name         | sex    | score | cid  | age  |
+------+--------------+--------+-------+------+------+
|  100 | wukong       | male   |    99 |    1 | NULL |
|  101 | wuneng       | male   |    59 |    1 | NULL |
|  102 | wujing       | male   |    60 |    1 | NULL |
|  103 | tangtang     | male   |   100 |    1 | NULL |
|  104 | baoyu        | male   |    17 |    2 | NULL |
|  105 | daiyu        | female |    16 |    2 | NULL |
|  106 | baichai      | female |    16 |    2 | NULL |
|  107 | xifeng       | female |    27 |    2 | NULL |
|  108 | liubei       | male   |    20 |    3 | NULL |
|  109 | sunquan      | male   |    20 |    3 | NULL |
|  110 | caocao       | male   |    20 |    3 | NULL |
|  111 | songjiang    | male   |    20 |    4 | NULL |
|  112 | likui        | male   |    22 |    4 | NULL |
|  113 | sunerniang   | female |    20 |    4 | NULL |
|  114 | zhouhuaqiang | male   |    50 |    9 |   30 |
+------+--------------+--------+-------+------+------+
15 rows in set (0.00 sec)
mysql> select * from clazz;
+------+------------+-----------------+
| cid  | cname      | cdesc           |
+------+------------+-----------------+
|    1 | computer   | computer desc   |
|    2 | software   | software desc   |
|    3 | math       | math desc       |
|    4 | biological | biological desc |
|    5 | education  | education desc  |
+------+------------+-----------------+
5 rows in set (0.00 sec)
mysql> select * from student s, clazz c where c.cid = s.cid;
+------+------------+--------+-------+------+------+------+------------+-----------------+
| sid  | name       | sex    | score | cid  | age  | cid  | cname      | cdesc           |
+------+------------+--------+-------+------+------+------+------------+-----------------+
|  100 | wukong     | male   |    99 |    1 | NULL |    1 | computer   | computer desc   |
|  101 | wuneng     | male   |    59 |    1 | NULL |    1 | computer   | computer desc   |
|  102 | wujing     | male   |    60 |    1 | NULL |    1 | computer   | computer desc   |
|  103 | tangtang   | male   |   100 |    1 | NULL |    1 | computer   | computer desc   |
|  104 | baoyu      | male   |    17 |    2 | NULL |    2 | software   | software desc   |
|  105 | daiyu      | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  106 | baichai    | female |    16 |    2 | NULL |    2 | software   | software desc   |
|  107 | xifeng     | female |    27 |    2 | NULL |    2 | software   | software desc   |
|  108 | liubei     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  109 | sunquan    | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  110 | caocao     | male   |    20 |    3 | NULL |    3 | math       | math desc       |
|  111 | songjiang  | male   |    20 |    4 | NULL |    4 | biological | biological desc |
|  112 | likui      | male   |    22 |    4 | NULL |    4 | biological | biological desc |
|  113 | sunerniang | female |    20 |    4 | NULL |    4 | biological | biological desc |
+------+------------+--------+-------+------+------+------+------------+-----------------+
14 rows in set (0.00 sec)

等值查询:条件中分别指定了两张表的关联关系

第一张表中114学生的cid是9

第二张表中5号班级在学生表中没有记录

所以最终等值查询只能得到14条记录

其中114的学生不会出现,以及cid为5的班级不会出现

约束

问题:在往数据库中创建好的表里面添加数据时,可不可以新增两行相同列值的数据

如果可行,会有什么弊端

  • 实体完整性约束

    • 表中的一行数据代表一个实体,实体完整性约束的作用时标志每一行的数据时不重复的,实体唯一

    • 主键约束:primary key,唯一,标识表中的一行数据,此列数据的值不可重复,且不能为null

      • create table tb_stu2(sid int primary key,name varchar(20),sex varchar(6),score double,age int
        );insert into tb_stu2 values(9527, 'huaan', 'male', 99, 20);
        insert into tb_stu2 values(9528, 'qiuxiang', 'female', 88, 18);
        insert into tb_stu2 values(9528, 'qiuxiang', 'female', 88, 18);			//X,sid为主键,不能重复
        insert into tb_stu2 values(null, 'chunxiang', 'female', 88, 18);		//X,sid为主键,不能为空null
    • 唯一约束:unique,唯一,标识表中的 一行数据,不能重复,可以为null

      • create table subject(sid int primary key,subName varchar(20) unique,subHour	int
        );insert into subject values(1, "java", 100);
        insert into subject values(2, "python", 50);
        insert into subject values(3, "java", 100);					//	X,不能添加相同的值给uniqueu约束的字段
        insert into subject values(4, , 100);
      • mysql中唯一约束时值互不相同,可以添加null作为其值,而且可以添加多个null

    • 自动增长列

      • auto_increment, 自动增长,给主键数值列添加自动增长,从1开始,每次加1,不能单独使用

      • create table subject02(sid int primary key auto_increment,subName varchar(20) unique,subHour	int
        );insert into subject02 values(1, "java", 100);
        insert into subject02 values(2, "pytyon", 90);
        insert into subject02 values(null, "ui", 80);
        insert into subject02(subname, subhour) values("bigdata", 80);
        insert into subject02(subname, subhour) values("bigdata99999999999999", 80);			//	X
        insert into subject02(subname, subhour) values("chat", 80);								//	有些版本会出现跳格

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/3943.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

中电金信:向“新”而行——探索融合架构的项目管理在保险行业的应用

近年来,险企在政策推动、市场牵引、自身发展、新技术应用日趋成熟等内外部因素的驱动下,积极投身到数字化转型的浪潮中。在拜访各类保险客户和合作项目的过程中,我们发现不少险企在数字化转型中或多或少都面临着战略如何落地、技术如何承接和…

wlan二层直连组网实验(ensp)

目录 1. VLAN 端口类型及参数设计2. IP 地址规划3. WLAN数据规划(1) DHCP服务器配置(2) AC 源接口地址、认证方式配置(3) AP 组的创建(4) 创建域管理模板、国家码认证(5) 创建安全模板(6) 创建SSID模板(7) 创建VAP模板(8) AP组绑定模板(9) 查看: 1. VLAN 端口类型及…

快手面试算法真题

按照html中的标签层数遍历节点名。 例如&#xff1a;html代码如下&#xff1a;(上面的数字表示层数) <!-- 1 --><div class"div1"><!-- 2 --><span class"span1"></span><!-- 2 --><p class"p1"><…

STC12C5A60S2单片机学习DAY.5 单片机寻址方式与指令

一、寻址方式 1、立即寻址 直接给出参与运算的操作数&#xff0c;不管此数字存放在哪个地址 如MOV A,#70H&#xff0c;即为将70H放入累加器A中 2、直接寻址 指令操作数域是参与运算的数的地址 如ANL 70H,#42H 表示地址为70H的数与立即数42H相与&#xff0c;得到的结果存…

43. UE5 RPG 实现敌人血量显示条

在上一篇文章中&#xff0c;我们实现了火球术伤害功能&#xff0c;在火球击中敌方目标&#xff0c;可以降低敌人20的血量&#xff0c;这个值现在是固定的&#xff0c;后面我们会修改火球的伤害设置。接着&#xff0c;我们也测试了功能是实现的&#xff0c;但是在正常的游玩过程…

untiy avpro播放超过8K视频的解决方案

安转LAV Filters解码器&#xff0c;然后指定Avpro使用这个解码器播放即可 第一步 安装解码器 下载链接 第二步 AVPro设置 MediaPlayer脚本中一共两处

网络安全实训Day17and18

写在前面 第17和18天都讲的sql注入&#xff0c;故合并 ​​​​​​ 网络空间安全实训-渗透测试 Web渗透 定义 针对Web站点的渗透攻击&#xff0c;以获取网站控制权限为目的 Web渗透的特点 Web技术学习门槛低&#xff0c;更容易实现 Web的普及性决定了Web渗透更容易找到目…

学习笔记-数据结构-线性表(2024-04-24)

对不带头节点的单链表进行就地倒置 函数的处理步骤如下&#xff1a; 初始化三个指针 p、q 和 r。p 用于追踪新链表的最后一个节点&#xff0c;最初设置为 NULL。q 指向当前正在处理的原链表的节点&#xff0c;最初是链表的头节点。r 用于临时保存 q->next&#xff0c;即下一…

C++之,static成员函数,友元函数以及内部类

个人主页&#xff1a;救赎小恶魔 欢迎大家来到小恶魔频道 好久不见&#xff0c;甚是想念 今天我们要深入讲述类与对象的初始化列表以及隐式类型转换 目录 引言&#xff1a; static成员函数 1. 概念 2.特性 友元 1.友元函数 2 友元类 内部类 匿名对象 引言&#xff…

批量插入,根据传入的值判断是否为null。不为null则插入,为null需要调用函数或者其他方式。

第一种 mybatis框架方式 java中&#xff0c;sql insert语句 批量添加 xml中 根据插入的值判断是否为null&#xff0c;不为null 则插入,为null需要调用函数插入 case when then 插入的值不是数据库中查询的值&#xff0c;是代码中参数传递过来的值 在Java中&#xff0c;如果你想…

杰发科技AC7840——ADC简介(1)_双路ADC同时使用

0. 简介 1. 特性 2. 双路ADC Sample里面没有双路的&#xff0c;以为那个规则组只有一个通道&#xff0c;看了外设寄存器才发现&#xff0c;原来他的通道是双路的。 注意1: ADC硬件引脚的配置 注意2: 规则组长度设置和 RSEQ序列号和CH通道号组合应该就对应了转换顺序&#xff0…

C语言Linux vim shell命令

无论是在插入模式或者是其他模式下对于文件的修改都是对于内存缓冲区进行修改&#xff0c;只有当点击w进行保存以后才会将数据写入到一个新的文件中的&#xff0c;将源文件删除&#xff0c;并且新文件改为文件的名字 1. actionmotion dG删到文件尾 ggdG先到开头再删除到末尾…

html+css+js+jquery实现在网页端将手动输入用户的信息转化成表格

1.实现的效果图 2.css代码 ​<style>*{background-color: antiquewhite;}#ss{font-size:20px;text-align: center;}#inputForm { margin-bottom: 20px; } #userTable { width: 100%; border-collapse: collapse; } #userTable th, #userTable td { border: 1px …

【uniapp/ucharts】采用 uniapp 框架的 h5 应用使用 ucharts(没有 uni_modules)

这种情况无法直接从 dcloud 平台上一键下载导入&#xff0c;所以应该在官网推荐的 git 仓库去单独下载&#xff1a; https://gitee.com/uCharts/uCharts/tree/master/uni-app/uCharts-%E7%BB%84%E4%BB%B6/qiun-data-charts(%E9%9D%9Euni_modules) 下载的文件是如图所示的路径&…

攻防世界 easyphp

本题主要利用的知识点是php绕过 一、PHP代码分析 首先先看一下代码 我们需要利用get方式上传3个参数a,b,c&#xff0c;这3个分别需要满足不同的条件: a&#xff1a;设置a值&#xff1b;值大于6000000&#xff1b;长度不超过3&#xff1b; b&#xff1a;设置b值&#xff1b;MD…

vue3前端调用后端接口实现批量删除

//删除的接口 export const deleteApi (address: string, methods: string, id: string) > { return instance<{ id: string }>({ url: address "/" id, method: methods, }); }; //批量删除 let selection ref([]) const handleSelectionChang…

【Qt】信号与槽

1 &#x1f351;信号和槽概述&#x1f351; 在 Qt 中&#xff0c;用户和控件的每次交互过程称为⼀个事件。⽐如 “⽤⼾点击按钮” 是⼀个事件&#xff0c;“⽤⼾关闭窗⼝” 也是⼀个事件。每个事件都会发出⼀个信号&#xff0c;例如⽤⼾点击按钮会发出 “按钮被点击” 的信号&…

Netty: NIO网络编程

文章目录 一、NIO介绍二、NIO原理三、Buffer四、Channel五、Selector 一、NIO介绍 NIO介绍 二、NIO原理 NIO有三大核心部分&#xff1a; 通道&#xff08;Channel&#xff09;、缓冲区&#xff08;Buffer&#xff09;和选择器&#xff08;Selector&#xff09;。Channel是对原…

深入探索CSS中的渐变艺术:线性渐变、径向渐变与锥形渐变详解

CSS3引入了多种渐变效果&#xff0c;使网页设计师能够在网页元素上实现平滑、连续的颜色过渡&#xff0c;大大丰富了视觉表达手段。在这篇文章中&#xff0c;我们将详细解读CSS3中的三种核心渐变类型&#xff1a;线性渐变&#xff08;Linear Gradients&#xff09;、径向渐变&a…

Golang爬虫代理接入的技术与实践

引言 随着互联网的迅猛发展&#xff0c;数据已经成为现代社会的重要资源之一。而网络爬虫作为一种数据采集工具&#xff0c;扮演着至关重要的角色。在Golang语言的生态系统中&#xff0c;开发者们可以借助其强大的并发特性和丰富的标准库&#xff0c;轻松构建高效稳健的网络爬…