Java基础入门day40

这篇具有很好参考价值的文章主要介绍了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,不能单独使用文章来源地址https://www.toymoban.com/news/detail-860427.html

      • 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);								//	有些版本会出现跳格

到了这里,关于Java基础入门day40的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Java基础面试题 1day

    基本数据类型根据所在 的作用域存放的位置是不一样的;当基本数据类型是局部变量时,存放的位置是栈;当基本数据类型是成员变量时,存放的位置是堆;当基本数据类型作为成员变量存在时,并且被static修饰时,存放的位置仍然是堆,但是类型就不属于是对象了,而是类

    2024年01月16日
    浏览(36)
  • Android adb shell 查看App内存(java堆内存/vss虚拟内存/详细的内存状况/内存快照hprof)和系统可用内存

    1.adb shell 获取app 进程的pid 根据某个渠道包,去查询对应的pid,如下所示: 2.通过adb shell 查看设备的java dalvik 堆内存的最大值 执行命令行: 若是app进程中java heap(dalvik heap size) 堆内存超过384m 就会抛出oom. 若是app mainfest.xml 中设置了largeHeap=“true”,则app 进程dalvik heap limit 对应

    2024年02月15日
    浏览(60)
  • 【Java】零基础上手SpringBoot学习日记(day1)

    此帖为本人学习Springboot时的笔记,由于是个接触计算机一年左右的新手,也没有网站开发经验,所以有些地方的理解会比较浅显并且可能会出现错误,望大佬们多多包涵和指正。 在我的理解中,Web应用的开发大体分为两个部分,一个是前端的页面设计以及实现,比如使用H

    2024年02月14日
    浏览(43)
  • 【零基础??天速成 Java】Day2 - 初识面向对象

    目录 前言 1. 可变参数的使用 2. 构造器 3. 包 1、包的创建 2、包的使用 3、包的命名规范 4、常用的包 5. 访问修饰符 6. 继承  7. super 8. 方法重写 Override 写在最后: 我的 java 基础学习,跟的是韩顺平的 java 课程~ 本篇文章涵盖视频 216 ~ 305 集里面 java 与 C++ 不同的知识

    2024年02月12日
    浏览(39)
  • 【100天精通python】Day40:GUI界面编程_PyQt 从入门到实战(完)_网络编程与打包发布

    目录  8 网络编程 8.1 使用PyQt 网络模块进行网络通信 服务器端示例 客户端示例

    2024年02月12日
    浏览(61)
  • 0基础学java-day26(满汉楼实战)

    2.1 用户登录 2.2 显示餐桌状态  2.3 预订 2.4 显示菜品  2.5 点餐 2.6 查看账单  2.7 结账 1.1 构建如下的项目结构 1.2 导入相关的工具类和jar包 相关的工具类和jar包会放在评论区的百度网盘的链接中 1.3 整体项目结构  1.4 相关sql语句,也就是要建立的表 一个类就对应一张表,如果

    2024年01月16日
    浏览(42)
  • 0基础学java-day27(正则表达式)

    为让大家对正则表达式底层实现有一个直观的映象,给大家举个实例 给你一段字符串 ( 文本 ), 请找出所有四个数字连在一起的子串, 比如 : 应该找到 1998 1999 3443 9889 === 分析底层实现 RegTheory.java 用于指定其前面的字符和组合项连续出现多少次 定位符 , 规定要匹配的字符串出

    2024年01月21日
    浏览(46)
  • JAVA基础学习笔记-day15-File类与IO流

    博文主要是自己学习JAVA基础中的笔记,供自己以后复习使用,参考的主要教程是B站的 尚硅谷宋红康2023大数据教程 老骥伏枥,志在千里 --曹操《龟虽寿》 1.1 概述 File类及本章下的各种流,都定义在 java.io 包下。 一个File对象代表硬盘或网络中可能存在的一个文件或者文件目

    2024年01月21日
    浏览(39)
  • JAVA基础学习笔记-day14-数据结构与集合源码2

    博文主要是自己学习JAVA基础中的笔记,供自己以后复习使用,参考的主要教程是B站的 尚硅谷宋红康2023大数据教程 君以此始,亦必以终。—左丘明《左传·宣公十二年》 7.1 List接口特点 List集合所有的元素是以一种 线性方式 进行存储的,例如,存元素的顺序是11、22、33。那

    2024年01月18日
    浏览(60)
  • 0基础学java-day25(JDBC 和数据库连接池)

    1.1 方式 1 1.2 方式 2  1.3 方式 3  1.4 方式 4  1.5 方式 5    1.6 课堂练习  2.1 基本介绍  2.2 应用实例 3.1 基本介绍  3.2 演示的sql语句  3.3 应用实例 ​ 4.1 基本介绍 4.2 预处理好处  4.3 应用案例  模拟经典的转账业务 【package utils】   【package domain】 【dao】 【test】

    2024年01月18日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包