標籤:join warnings set tab prim 多表 直接 顯示 外鍵
建立外鍵mysql> CREATE TABLE `study_record` ( -> `id` int(11) NOT NULL, -> `day` int NOT NULL, -> `status` char(32) NOT NULL, -> `stu_id` int(11) NOT NULL, -> PRIMARY KEY (`id`), -> KEY `fk_student_key` (`stu_id`), -> CONSTRAINT `fk_student_key` FOREIGN KEY (`stu_id`) REFERENCES `student` (`id`) -> );Query OK, 0 rows affected (0.05 sec)mysql> alter table study_record modify id int auto_increment;Query OK, 0 rows affected (0.07 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> insert into study_record (day,status,stu_id) values(1,'Yes',4);Query OK, 1 row affected (0.00 sec)mysql> insert into study_record (day,status,stu_id) values(1,'Yes',1);Query OK, 1 row affected (0.00 sec)mysql> mysql> select * from study_record;+----+-----+--------+--------+| id | day | status | stu_id |+----+-----+--------+--------+| 1 | 1 | Yes | 4 || 2 | 1 | Yes | 1 |+----+-----+--------+--------+2 rows in set (0.00 sec)mysql> insert into study_record (day,status,stu_id) values(2,'No',1);Query OK, 1 row affected (0.01 sec)mysql> select * from study_record;+----+-----+--------+--------+| id | day | status | stu_id |+----+-----+--------+--------+| 1 | 1 | Yes | 4 || 2 | 1 | Yes | 1 || 3 | 2 | No | 1 |+----+-----+--------+--------+3 rows in set (0.00 sec)mysql> select * from study_record;+----+-----+--------+--------+| id | day | status | stu_id |+----+-----+--------+--------+| 1 | 1 | Yes | 4 || 2 | 1 | Yes | 1 || 3 | 2 | No | 1 |+----+-----+--------+--------+3 rows in set (0.00 sec)mysql> delete from study_record where id=3;Query OK, 1 row affected (0.02 sec)mysql> select * from study_record;+----+-----+--------+--------+| id | day | status | stu_id |+----+-----+--------+--------+| 1 | 1 | Yes | 4 || 2 | 1 | Yes | 1 |+----+-----+--------+--------+2 rows in set (0.00 sec)mysql> 多表查詢:mysql> create table A( -> a int not null);Query OK, 0 rows affected (0.04 sec)mysql> create table B( b int not null);Query OK, 0 rows affected (0.02 sec)mysql> insert into A (a) values(1);Query OK, 1 row affected (0.01 sec)mysql> insert into A (a) values(2);Query OK, 1 row affected (0.00 sec)mysql> insert into A (a) values(3);Query OK, 1 row affected (0.00 sec)mysql> insert into A (a) values(4);Query OK, 1 row affected (0.00 sec)mysql> insert into B (b) values(3);Query OK, 1 row affected (0.02 sec)mysql> insert into B (b) values(4);Query OK, 1 row affected (0.00 sec)mysql> insert into B (b) values(5);Query OK, 1 row affected (0.00 sec)mysql> insert into B (b) values(6);Query OK, 1 row affected (0.00 sec)mysql> insert into B (b) values(7);Query OK, 1 row affected (0.00 sec)mysql> select * from A;+---+| a |+---+| 1 || 2 || 3 || 4 |+---+4 rows in set (0.00 sec)mysql> select * from B;+---+| b |+---+| 3 || 4 || 5 || 6 || 7 |+---+5 rows in set (0.00 sec)Inner join文法 其實就是只顯示2個表的交集mysql> select * from A inner join B on A.a = B.b;+---+---+| a | b |+---+---+| 3 | 3 || 4 | 4 |+---+---+2 rows in set (0.00 sec)第二種文法mysql> select A.*,B.* from A,B where A.a=B.b;+---+---+| a | b |+---+---+| 3 | 3 || 4 | 4 |+---+---+2 rows in set (0.00 sec)left join 文法求差級mysql> select * from A left join B on A.a = B.b;+---+------+| a | b |+---+------+| 3 | 3 || 4 | 4 || 1 | NULL || 2 | NULL |+---+------+4 rows in set (0.00 sec)mysql> select * from B left join A on A.a = B.b;+---+------+| b | a |+---+------+| 3 | 3 || 4 | 4 || 5 | NULL || 6 | NULL || 7 | NULL |+---+------+5 rows in set (0.00 sec)right joinmysql> select * from A right join B on A.a = B.b;+------+---+| a | b |+------+---+| 3 | 3 || 4 | 4 || NULL | 5 || NULL | 6 || NULL | 7 |+------+---+5 rows in set (0.01 sec)full join 求並集 mysql 並不直接支援full join,but 總是難不到我們mysql> select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b;+------+------+| a | b |+------+------+| 3 | 3 || 4 | 4 || 1 | NULL || 2 | NULL || NULL | 5 || NULL | 6 || NULL | 7 |+------+------+7 rows in set (0.01 sec)mysql>
MySQL插入外鍵