When there is at least one match in the table, the INNER JOIN keyword returns a row.
MySQL Tutorials >
Mysql> CREATE TABLE Books (
-> BookID smallint NOT null primary key,
-> booktitle varchar () NOT NULL,
-> copyright year is not null
->)
-> Engine=innodb;
Query OK, 0 rows affected (0.09 sec)
Mysql>
Mysql>
Mysql> INSERT into the books values (12786, ' Java ', 1934),
-> (13331, ' MySQL ', 1919),
-> (14356, ' PHP Tutorial ', 1966),
-> (15729, ' Perl ', 1932),
-> (16284, ' Oracle ', 1996),
-> (17695, ' Pl/sql ', 1980),
-> (19264, ' Web Effects ', 1992),
-> (19354, ' Www.111cn.net ', 1993);
Query OK, 8 rows affected (0.05 sec)
Records:8 duplicates:0 warnings:0
Mysql>
Mysql>
Mysql> CREATE TABLE authors (
-> Authid smallint NOT null primary key,
-> authfn varchar (20),
-> authmn varchar (20),
-> authln varchar (20)
->)
-> Engine=innodb;
Query OK, 0 rows affected (0.05 sec)
Mysql>
Mysql>
Mysql> INSERT into authors values (1006, ' h ', ' s. ', ' t '),
-> (1007, ' J ', ' C ', ' O '),
-> (1008, ' B ', null, ' E '),
-> (1009, ' R ', ' m ', ' R '),
-> (1010, ' J ', ' K ', ' t '),
-> (1011, ' j ', ' G. ', ' n '),
-> (1012, ' a ', null, ' P '),
-> (1013, ' a ', null, ' W '),
-> (1014, ' n ', null, ' a ');
Query OK, 9 rows affected (0.03 sec)
Records:9 duplicates:0 warnings:0
Mysql>
Mysql>
Mysql> CREATE TABLE Authorbook (
-> Authid smallint NOT NULL,
-> BookID smallint NOT NULL,
-> primary KEY (Authid, BookID),
-> foreign KEY (Authid) references authors (Authid),
-> foreign KEY (BookID) References books (BookID)
->)
-> Engine=innodb;
Query OK, 0 rows affected (0.06 sec)
Mysql>
Mysql>
mysql> INSERT into Authorbook values (1006, 14356),
-> (1008, 15729),
-> (1009, 12786),
-> (1010, 17695),
-> (1011, 15729),
-> (1012, 19264),
-> (1012, 19354),
-> (1014, 16284);
Query OK, 8 rows affected (0.05 sec)
Records:8 duplicates:0 warnings:0
Mysql>
Mysql>
Mysql> SELECT * from authors;
+--------+--------+--------+--------+
| Authid | AUTHFN | Authmn | AUTHLN |
+--------+--------+--------+--------+
| 1006 | H | S. | T |
| 1007 | J | C | o |
| 1008 | B | null | e |
| 1009 | R | m | R |
| 1010 | J | K | T |
| 1011 | J | G. | n |
| 1012 | A | null | P |
| 1013 | A | null | W |
| 1014 | n | null | A |
+--------+--------+--------+--------+
9 Rows in Set (0.00 sec)
Mysql> select * from books;
+--------+----------------+-----------+
| BookID | BookTitle | Copyright |
+--------+----------------+-----------+
| 12786 | java | 1934 |
| 13331 | MySQL | 1919 |
| 14356 | php | 1966 |
| 15729 | Perl | 1932 |
| 16284 | Oracle | 1996 |
| 17695 | Pl/sql | 1980 |
| 19264 | JavaScript | 1992 |
| 19354 | www.111cn.net | 1993 |
+--------+----------------+-----------+
8 rows in Set (0.00 sec)
Mysql> select * from Authorbook;
+--------+--------+
| Authid | BookID |
+--------+--------+
| 1009 | 12786 |
| 1006 | 14356 |
| 1008 | 15729 |
| 1011 | 15729 |
| 1014 | 16284 |
| 1010 | 17695 |
| 1012 | 19264 |
| 1012 | 19354 |
+--------+--------+
8 rows in Set (0.00 sec)
Mysql>
Mysql>
Mysql> Select BookTitle, Authid from the books inner join Authorbook;
+----------------+--------+
| BookTitle | Authid |
+----------------+--------+
| java | 1006 |
| MySQL | 1006 |
| php | 1006 |
| Perl | 1006 |
| Oracle | 1006 |
| Pl/sql | 1006 |
| JavaScript | 1006 |
| www.111cn.net | 1006 |
| java | 1008 |
| MySQL | 1008 |
| php | 1008 |
| Perl | 1008 |
| Oracle | 1008 |
| Pl/sql | 1008 |
| JavaScript | 1008 |
| www.111cn.net | 1008 |
| java | 1009 |
| MySQL | 1009 |
| php | 1009 |
| Perl | 1009 |
| Oracle | 1009 |
| Pl/sql | 1009 |
| JavaScript | 1009 |
| www.111cn.net | 1009 |
| java | 1010 |
| MySQL | 1010 |
| php | 1010 |
| Perl | 1010 |
| Oracle | 1010 |
| Pl/sql | 1010 |
| JavaScript | 1010 |
| www.111cn.net | 1010 |
| java | 1011 |
| MySQL | 1011 |
| php | 1011 |
| Perl | 1011 |
| Oracle | 1011 |
| Pl/sql | 1011 |
| JavaScript | 1011 |
| www.111cn.net | 1011 |
| java | 1012 |
| MySQL | 1012 |
| php | 1012 |
| Perl | 1012 |
| Oracle | 1012 |
| Pl/sql | 1012 |
| JavaScript | 1012 |
| www.111cn.net | 1012 |
| java | 1012 |
| MySQL | 1012 |
| php | 1012 |
| Perl | 1012 |
| Oracle | 1012 |
| Pl/sql | 1012 |
| JavaScript | 1012 |
| www.111cn.net | 1012 |
| java | 1014 |
| MySQL | 1014 |
| php | 1014 |
| Perl | 1014 |
| Oracle | 1014 |
| Pl/sql | 1014 |
| JavaScript | 1014 |
| www.111cn.net | 1014 |
+----------------+--------+
Rows in Set (0.00 sec)
Mysql>
mysql> drop table Authorbook;
Query OK, 0 rows affected (0.02 sec)
mysql> drop table books;
Query OK, 0 rows affected (0.06 sec)
mysql> drop table authors;
Query OK, 0 rows affected (0.03 sec)