SQL INNER JOIN Usage Resolution

Source: Internet
Author: User
Tags joins php tutorial

When there is at least one match in the table, the INNER JOIN keyword returns a row.

INNER JOIN keyword syntax
Select COLUMN_NAME (s)
From table_name1
INNER JOIN Table_name2
On Table_name1.column_name=table_name2.column_name
Note: The inner join is the same as the join, and the INNER JOIN keyword returns rows when there is at least one match in the table. If the rows in "a" do not match in "B", the rows are not listed.


Multi-table Joint query

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)

Two table connections

Mysql> Select Employee.first_name, Job.title, Duty.task
-> from employee, job, duty
-> WHERE (employee.id = job.id and employee.id = duty.id);
+------------+------------+-----------+
| first_name | Title | Task |
+------------+------------+-----------+
| Jason | Tester | Test |
| Alison | Accountant | Calculate |
| James | Developer | Program |
| Celia | Coder | Test |
| Robert | Director | Manage |
| Linda | Mediator | Talk |
| David | Proffessor | Speak |
| James | Programmer | Shout |
+------------+------------+-----------+
8 rows in Set (0.00 sec)

Mysql>
Mysql>
Mysql>
Mysql> drop table duty;
Query OK, 0 rows Affected (0.00 sec)

mysql> drop table job;
Query OK, 0 rows affected (0.01 sec)

mysql> drop table employee;
Query OK, 0 rows Affected (0.00 sec)


Summarize

Inner JOIN joins two datasheet usage:
SELECT * FROM table 1 inner JOIN table 2 on table 1. Field number = Table 2. Field number

Inner JOIN joins three datasheet usage:
SELECT * FROM (table 1 inner JOIN table 2 on table 1. Field number = Table 2. Field number) inner JOIN table 3 on table 1. Field number = Table 3. Field number

Inner JOIN joins four datasheet usage:
SELECT * FROM (table 1 inner JOIN table 2 on table 1. Field number = Table 2. Field number) inner JOIN table 3 on table 1. Field number = Table 3. Field number) inner JOIN table 4 on member. Field number = Table 4.

Inner JOIN joins five datasheet usage:
SELECT * FROM ((Table 1 inner JOIN table 2 on table 1. Field number = Table 2. Field number) inner JOIN table 3 on table 1. Field number = Table 3. Field number) inner JOIN table 4 on member. Field number = Table 4. Field No. NER Join table 5 on member. Field number = Table 5. Field number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.